读书人

用注脚的方式配置hibernate po对象关系

发布时间: 2012-08-26 16:48:06 作者: rapoo

用注解的方式配置hibernate po对象关系

1.一对多 这个我们很容易想到的 mappedBy的意思是关联外键 如下面的意思是:Employee 中的外键是Employee中的department 默认是与主键关联 如果我们在一端不写则会生成一个中间表。employee_department 因为是由两个主键来维护。 @Entity public class Employee{@id@GeneratedValue(strategy=GenerationType.AUTO)private int empid;private String ename;@ManyToOneprivate Department department; // get set 省略}@Entitypublic class Department{@Id@GeneratedValue(strategy=GenerationType.AUTO)private int id;private String dname;private String location;@OneToMany(mappedBy="department")private Set<Employee> employees=new HashSet<Employee>();//get set 方法省略}--------------------------2.多对多 多对多会生成中间表,而中间表若含有别的字段则没有办法操作,一般使用中我们将其分为两个一对多.

读书人网 >编程

热点推荐