1 Hibernate的一对多关联映射
核心代码:
- <setname="students">
- <keycolumn="classesId"/>
- <one-to-manyclass="Students"/>
- </set>
<set name="students"> <key column="classesId"/> <one-to-many class="Students"/> </set>
hihernate一对多关联映射(单向Classes----->Student)
一对多关联映射利用了多对一关联映射原理
多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一
一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多
也就是说一对多和多对一的映射策略是一样的,只是站的角度不同
在一端维护关系的缺点:
* 如果将t_student表里的classesid字段设置为非空,则无法保存
* 因为不是在student这一端维护关系,所以student不知道是哪个班的,
所以需要发出多余的update语句来更新关系
一:实体类:
- package com.tao.hibernate.one2many;
- import java.util.Set;
- public class Classes {
- private Integer id;
- private String name;
- private Set<Students> students;
- public Classes() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Students> getStudents() {
- return students;
- }
- public void setStudents(Set<Students> students) {
- this.students = students;
- }
- }
package com.tao.hibernate.one2many; import java.util.Set; public class Classes { private Integer id; private String name; private Set<Students> students; public Classes() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Students> getStudents() { return students; } public void setStudents(Set<Students> students) { this.students = students; } }
- package com.tao.hibernate.one2many;
- public class Students {
- private Integer id;
- private String name;
- private Classes classes;
- public Students() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Classes getClasses() {
- return classes;
- }
- public void setClasses(Classes classes) {
- this.classes = classes;
- }
- }
package com.tao.hibernate.one2many; public class Students { private Integer id; private String name; private Classes classes; public Students() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
二:映射文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.one2many">
- <class name="Classes" table="t_classes">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" />
- <set name="students">
- <key column="classId"/>
- <one-to-many class="Students"/>
- </set>
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.one2many"> <class name="Classes" table="t_classes"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" column="name" /> <set name="students"> <key column="classId"/> <one-to-many class="Students"/> </set> </class> </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.one2many">
- <class name="Students" table="t_students">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" />
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.one2many"> <class name="Students" table="t_students"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" column="name" /> </class> </hibernate-mapping>
三:测试类:
- package com.tao.hibernate.one2many;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import com.tao.hibernate.HibernateUtils;
- public class TestMain {
- Session session = HibernateUtils.getFactory().openSession();
- @Test
- public void testSave() {
- Students s1 = new Students();
- s1.setName("tao");
- Students s2 = new Students();
- s2.setName("war");
- Classes c = new Classes();
- c.setName("148班");
- Set<Students> students = new HashSet<Students>();
- students.add(s1);
- students.add(s2);
- c.setStudents(students);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(s1);
- session.save(s2);
- session.save(c);
- ts.commit();
- }
- @Test
- public void testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- Classes c = (Classes) session.load(Classes.class, 1);
- for (Iterator<Students> iter = c.getStudents().iterator(); iter
- .hasNext();) {
- System.out.println(iter.next().getName());
- }
- }
- }
2 Hibernate的一对多和多对一双向关联映射
核心代码:
<many-to-one name="classes" column="classesId"/>
<set name="students" inverse="true" cascade="all">
<key column="classesId" />
<one-to-many class="Students" />
</set>
hihernate一对多关联映射(双向Classes<----->Student)
一对多双向关联映射:
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一一端
* 在多一端采用<many-to-one>
注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误
如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多
的一端来维护关联关系
关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系
注意:inverse属性,只影响数据的存储,也就是持久化
inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应
一:实体类:
- package com.tao.hibernate.one2many2;
- import java.util.Set;
- public class Classes {
- private Integer id;
- private String name;
- private Set<Students> students;
- public Classes() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Students> getStudents() {
- return students;
- }
- public void setStudents(Set<Students> students) {
- this.students = students;
- }
- }
package com.tao.hibernate.one2many2; import java.util.Set; public class Classes { private Integer id; private String name; private Set<Students> students; public Classes() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Students> getStudents() { return students; } public void setStudents(Set<Students> students) { this.students = students; } }
- package com.tao.hibernate.one2many2;
- public class Students {
- private Integer id;
- private String name;
- private Classes classes;
- public Students() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Classes getClasses() {
- return classes;
- }
- public void setClasses(Classes classes) {
- this.classes = classes;
- }
- }
package com.tao.hibernate.one2many2; public class Students { private Integer id; private String name; private Classes classes; public Students() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
二:映射文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false">
- <class name="Classes" table="t_classes">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" />
- <set name="students" inverse="true" cascade="all">
- <key column="classesId" />
- <one-to-many class="Students" />
- </set>
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false"> <class name="Classes" table="t_classes"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" column="name" /> <set name="students" inverse="true" cascade="all"> <key column="classesId" /> <one-to-many class="Students" /> </set> </class> </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false">
- <class name="Students" table="t_students">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" />
- <many-to-one name="classes" column="classesId"/>
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false"> <class name="Students" table="t_students"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" column="name" /> <many-to-one name="classes" column="classesId"/> </class> </hibernate-mapping>
三:测试类:
- package com.tao.hibernate.one2many2;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import com.tao.hibernate.HibernateUtils;
- public class TestMain {
- Session session = HibernateUtils.getFactory().openSession();
- @Test
- public void testSave() {
- Students s1 = new Students();
- s1.setName("tao");
- Students s2 = new Students();
- s2.setName("war");
- Classes c = new Classes();
- c.setName("148班");
- Set<Students> students = new HashSet<Students>();
- students.add(s1);
- students.add(s2);
- c.setStudents(students);
- s1.setClasses(c);
- s2.setClasses(c);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(c);
- ts.commit();
- }
- @Test
- public void testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- Classes c = (Classes) session.load(Classes.class, 1);
- for (Iterator<Students> iter = c.getStudents().iterator(); iter
- .hasNext();) {
- System.out.println(iter.next().getName());
- }
- }
- @Test
- public void testLoad2() {
- Session session = HibernateUtils.getFactory().openSession();
- Students s = (Students) session.load(Students.class, 1);
- System.out.println(s.getName());
- System.out.println(s.getClasses().getName());
- }
- }
3.Hibernate的多对多单向关联映射
hibernate多对多关联映射(单向User---->Role)
具体映射方式:
<set name="roles" table="t_user_role">
<key column="userid"/>
<many-to-many class="Role" column="roleid"/>
</set>
一:实体类:
- package com.tao.hibernate.many2many;
- public class Role {
- private Integer id;
- private String name;
- public Role() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
package com.tao.hibernate.many2many; public class Role { private Integer id; private String name; public Role() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- package com.tao.hibernate.many2many;
- import java.util.Set;
- public class User {
- private Integer id;
- private String name;
- private Set<Role> roles;
- public User() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Role> getRoles() {
- return roles;
- }
- public void setRoles(Set<Role> roles) {
- this.roles = roles;
- }
- }
package com.tao.hibernate.many2many; import java.util.Set; public class User { private Integer id; private String name; private Set<Role> roles; public User() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
二:映射文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.many2many">
- <class name="Role" table="t_role">
- <id name="id" column="id" type="integer">
- <generator class="native">
- </generator>
- </id>
- <property name="name" column="name" />
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.many2many"> <class name="Role" table="t_role"> <id name="id" column="id" type="integer"> <generator class="native"> </generator> </id> <property name="name" column="name" /> </class> </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.tao.hibernate.many2many"
- auto-import="false">
- <class name="User" table="t_user">
- <id name="id" column="id" type="integer">
- <generator class="native">
- </generator>
- </id>
- <property name="name" column="name" />
- <set name="roles" table="t_user_role" cascade="all">
- <key column="userId" />
- <many-to-many class="Role" column="roleId" />
- </set>
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tao.hibernate.many2many" auto-import="false"> <class name="User" table="t_user"> <id name="id" column="id" type="integer"> <generator class="native"> </generator> </id> <property name="name" column="name" /> <set name="roles" table="t_user_role" cascade="all"> <key column="userId" /> <many-to-many class="Role" column="roleId" /> </set> </class> </hibernate-mapping>
三:测试类:
- package com.tao.hibernate.many2many;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import com.tao.hibernate.HibernateUtils;
- public class TestMain {
- Session session = HibernateUtils.getFactory().openSession();
- @Test
- public void testSave() {
- User u1 = new User();
- u1.setName("u1");
- User u2 = new User();
- u2.setName("u2");
- User u3 = new User();
- u3.setName("u3");
- Role r1 = new Role();
- r1.setName("admin");
- Role r2 = new Role();
- r2.setName("guest");
- Set<Role> roles1 = new HashSet<Role>();
- roles1.add(r1);
- Set<Role> roles2 = new HashSet<Role>();
- roles2.add(r2);
- Set<Role> roles3 = new HashSet<Role>();
- roles3.add(r1);
- roles3.add(r2);
- u1.setRoles(roles1);
- u2.setRoles(roles2);
- u3.setRoles(roles3);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(u1);
- session.save(u2);
- session.save(u3);
- ts.commit();
- }
- @Test
- public void testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- User u1 = (User) session.load(User.class, 1);
- System.out.println(u1.getName());
- for (Iterator<Role> iter = u1.getRoles().iterator(); iter.hasNext();) {
- System.out.println(iter.next().getName());
- }
- }
- }