Hibernate应用One2Many
size=x-large]
1、一对多或多对一(Group&Person 单向)[/size]
一个团队对应多名成员或多名成员对应一个团队,从单向来操作(主要看Annotation注解@):
package com.sean.model.one2many;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* 一对多:团队对象
*
* @author Sean 2013-2-26
*
*/
@Entity
@Table(name = "p_group")
public class Group {
private int id;
private String name;
private Set<Person> pset;
@OneToMany
public Set<Person> getPset() {
return pset;
}
public void setPset(Set<Person> pset) {
this.pset = pset;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注意:@Table(name = "p_group")这里配置了与数据库不同的表名,原因是Hibernate在建立SQL语句时会将我们的Group类名,误解为Group By SQL语句,所以如果这里不这样配置一下就会报错。
package com.sean.model.one2many;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* 一对多:成员对象
*
* @author Sean 2013-2-26
*
*/
@Entity
public class Person {
private int id;
private String name;
private Group group;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
从这里可以看出一对多或多对一,只需要对为一的那边设置@OneToMany,可想而知,双向的操作就是对为多的那边设置@ManyToOne就可以了,请看下面。
2、一对多或多对一(Group&Person 双向)
一个团队对应多名成员或多名成员对应一个团队,从双向来操作(主要看Annotation注解@):
Group类:
package com.sean.model.one2many;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* 一对多:团队对象
*
* @author Sean 2013-2-26
*
*/
@Entity
@Table(name = "p_group")
public class Group {
private int id;
private String name;
private Set<Person> pset;
@OneToMany
public Set<Person> getPset() {
return pset;
}
public void setPset(Set<Person> pset) {
this.pset = pset;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Person类:
package com.sean.model.one2many;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* 一对多:成员对象
*
* @author Sean 2013-2-26
*
*/
@Entity
public class Person {
private int id;
private String name;
private Group group;
@ManyToOne
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}