一对一关联: 1.一对一单向外间关联 a)Annotation: @OneToOne @JoinColumn(name="该类对应的数据库表中,外键的名称") *示例:在Husband类中 @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } b)xml:<many-to-one unique> 2.一对一双向关联 a)Annotation: @OneToOne(mappedBy="") *示例:Husband与Wife一对一双向关联 在Husband类中 @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } 在wife类中 @OneToOne(mappedBy="wife") public Husband getHusband() { return husband; } ****只要双向关联,必设mappedBy(name="") b)xml: <many-to-one name="student" column="studentId" unique="true"></many-to-one> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> 示例:StuIdCard与Student一对一双向关联 在StuIdCard.hbm.xml配置文件中: <class name="com.bjsxt.hibernate.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"> </many-to-one> </class> 在Student.hbm.xml中 <class name="com.bjsxt.hibernate.Student" dynamic-update="true"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age" /> <property name="sex" /> <property name="good" type="yes_no"></property> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> 3.联合主键映射 a)Annotation: @JoinColumns( @JoinColumn(name="wifeId", referencedColumnName="id"), @JoinColumn(name="wifeName", referencedColumnName="name") ) Wife为联合主键(主键为id和name),WifePK主键类 **Wife类 @Entity @IdClass(WifePK.class) public class Wife { private int id; private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Id public int getId() { return id; } public void setId(int id) { this.id = id; } @Id public String getName() { return name; } public void setName(String name) { this.name = name; } } **WifePK类 public class WifePK implements Serializable {//**注意要继承Serializable并且重写xxx方法 private int id; private String name; 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; } } **Husband类 @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne @JoinColumns( { @JoinColumn(name="wifeId", referencedColumnName="id"), @JoinColumn(name="wifeName", referencedColumnName="name") } ) public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } } 4.组件映射 a)Annotation中: Husband 中的一个属性是Wife类 组件映射,一对一的,将Husband与Wife的属性放到一张表中 Husband类: @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @Embedded public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } } Wife类: 不用配置! b)xml中: <class name="com.bjsxt.hibernate.Husband" > <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <component name="wife"> <property name="wifeName"></property> <property name="age"></property> </component> </class> 5.多对一单向关联 :在多的一方增加外键指向一的那方 **Group 与 User 多对一关系 a)Annotation: @Entity @Table(name="t_group") public class Group { private int id; private String name; @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; } } @Entity @Table(name="t_user") public class User { private int id; private String name; private Group group; @ManyToOne @JoinColum(name="xxx")//映射成为其他名称 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; } } b)xml配置文件 **User.hdb.xml <class name="com.bjsxt.hibernate.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <many-to-one name="group" column="groupId" /> </class> **Group.hdb.xml <class name="com.bjsxt.hibernate.Group" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> 6.一对多单向关联 Group类中有一个属性是多那一方对象的集合 一般为HashSet对象 a)Annotation: @Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @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 @JoinColumn(name="groupId") ****注意如果只写OneToMany 不指定多那一方关联的外键的话,hibernate默认作为多对多处理, 即将会建立一张中间表 public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } } @Entity @Table(name="t_user") public class User { private int id; private String name; @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; } } 7.一对多,多对一双向关联 a)Annotation" @Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @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(mappedBy="group") public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } } @Entity @Table(name="t_user") public class User { 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; } } b)xml: 8.多对多单向关系映射 @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @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; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } } @Entity public class Student { private int id; private String name; @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; } } 9.多对多双向关系映射 @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @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; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } } @Entity public class Student { private int id; private String name; private Set<Teacher> teachers = new HashSet<Teacher>(); @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } @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; } }
hibernate 关联关系配置
最新推荐文章于 2021-10-30 10:51:35 发布