HiberNate学习-3 映射关系

1 Hibernate的一对多关联映射

核心代码:

Xml代码 复制代码 收藏代码
  1. <setname="students">
  2. <keycolumn="classesId"/>
  3. <one-to-manyclass="Students"/>
  4. </set>
<set name="students">
	<key column="classesId"/>
	<one-to-many class="Students"/>
</set>

 

hihernate一对多关联映射(单向Classes----->Student)

一对多关联映射利用了多对一关联映射原理

多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一
一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多

也就是说一对多和多对一的映射策略是一样的,只是站的角度不同

在一端维护关系的缺点:
* 如果将t_student表里的classesid字段设置为非空,则无法保存
* 因为不是在student这一端维护关系,所以student不知道是哪个班的,
所以需要发出多余的update语句来更新关系

 

一:实体类:

Classes.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many;
  2. import java.util.Set;
  3. public class Classes {
  4. private Integer id;
  5. private String name;
  6. private Set<Students> students;
  7. public Classes() {
  8. super();
  9. }
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public Set<Students> getStudents() {
  23. return students;
  24. }
  25. public void setStudents(Set<Students> students) {
  26. this.students = students;
  27. }
  28. }
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;
	}

}
Students.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many;
  2. public class Students {
  3. private Integer id;
  4. private String name;
  5. private Classes classes;
  6. public Students() {
  7. super();
  8. }
  9. public Integer getId() {
  10. return id;
  11. }
  12. public void setId(Integer id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public Classes getClasses() {
  22. return classes;
  23. }
  24. public void setClasses(Classes classes) {
  25. this.classes = classes;
  26. }
  27. }
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;
	}

}

二:映射文件:

Classes.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.one2many">
  5. <class name="Classes" table="t_classes">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="name" column="name" />
  10. <set name="students">
  11. <key column="classId"/>
  12. <one-to-many class="Students"/>
  13. </set>
  14. </class>
  15. </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>

 

Students.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.one2many">
  5. <class name="Students" table="t_students">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="name" column="name" />
  10. </class>
  11. </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>

三:测试类:

Testmain.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import org.hibernate.Session;
  6. import org.hibernate.Transaction;
  7. import org.junit.Test;
  8. import com.tao.hibernate.HibernateUtils;
  9. public class TestMain {
  10. Session session = HibernateUtils.getFactory().openSession();
  11. @Test
  12. public void testSave() {
  13. Students s1 = new Students();
  14. s1.setName("tao");
  15. Students s2 = new Students();
  16. s2.setName("war");
  17. Classes c = new Classes();
  18. c.setName("148班");
  19. Set<Students> students = new HashSet<Students>();
  20. students.add(s1);
  21. students.add(s2);
  22. c.setStudents(students);
  23. Transaction ts = session.beginTransaction();
  24. ts.begin();
  25. session.save(s1);
  26. session.save(s2);
  27. session.save(c);
  28. ts.commit();
  29. }
  30. @Test
  31. public void testLoad() {
  32. Session session = HibernateUtils.getFactory().openSession();
  33. Classes c = (Classes) session.load(Classes.class, 1);
  34. for (Iterator<Students> iter = c.getStudents().iterator(); iter
  35. .hasNext();) {
  36. System.out.println(iter.next().getName());
  37. }
  38. }
  39. }

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操作上的连锁反应

 

一:实体类:

Classes.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many2;
  2. import java.util.Set;
  3. public class Classes {
  4. private Integer id;
  5. private String name;
  6. private Set<Students> students;
  7. public Classes() {
  8. super();
  9. }
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public Set<Students> getStudents() {
  23. return students;
  24. }
  25. public void setStudents(Set<Students> students) {
  26. this.students = students;
  27. }
  28. }
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;
	}

}

 

Students.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many2;
  2. public class Students {
  3. private Integer id;
  4. private String name;
  5. private Classes classes;
  6. public Students() {
  7. super();
  8. }
  9. public Integer getId() {
  10. return id;
  11. }
  12. public void setId(Integer id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public Classes getClasses() {
  22. return classes;
  23. }
  24. public void setClasses(Classes classes) {
  25. this.classes = classes;
  26. }
  27. }
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;
	}

}

 

二:映射文件:

Classes.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false">
  5. <class name="Classes" table="t_classes">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="name" column="name" />
  10. <set name="students" inverse="true" cascade="all">
  11. <key column="classesId" />
  12. <one-to-many class="Students" />
  13. </set>
  14. </class>
  15. </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>

 

Students.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.one2many2" auto-import="false">
  5. <class name="Students" table="t_students">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="name" column="name" />
  10. <many-to-one name="classes" column="classesId"/>
  11. </class>
  12. </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>

 

三:测试类:

Testmain.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2many2;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import org.hibernate.Session;
  6. import org.hibernate.Transaction;
  7. import org.junit.Test;
  8. import com.tao.hibernate.HibernateUtils;
  9. public class TestMain {
  10. Session session = HibernateUtils.getFactory().openSession();
  11. @Test
  12. public void testSave() {
  13. Students s1 = new Students();
  14. s1.setName("tao");
  15. Students s2 = new Students();
  16. s2.setName("war");
  17. Classes c = new Classes();
  18. c.setName("148班");
  19. Set<Students> students = new HashSet<Students>();
  20. students.add(s1);
  21. students.add(s2);
  22. c.setStudents(students);
  23. s1.setClasses(c);
  24. s2.setClasses(c);
  25. Transaction ts = session.beginTransaction();
  26. ts.begin();
  27. session.save(c);
  28. ts.commit();
  29. }
  30. @Test
  31. public void testLoad() {
  32. Session session = HibernateUtils.getFactory().openSession();
  33. Classes c = (Classes) session.load(Classes.class, 1);
  34. for (Iterator<Students> iter = c.getStudents().iterator(); iter
  35. .hasNext();) {
  36. System.out.println(iter.next().getName());
  37. }
  38. }
  39. @Test
  40. public void testLoad2() {
  41. Session session = HibernateUtils.getFactory().openSession();
  42. Students s = (Students) session.load(Students.class, 1);
  43. System.out.println(s.getName());
  44. System.out.println(s.getClasses().getName());
  45. }
  46. }

3.Hibernate的多对多单向关联映射  

 

 

hibernate多对多关联映射(单向User---->Role)

具体映射方式:
<set name="roles" table="t_user_role">
<key column="userid"/>
<many-to-many class="Role" column="roleid"/>
</set>

 

一:实体类:

Role.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2many;
  2. public class Role {
  3. private Integer id;
  4. private String name;
  5. public Role() {
  6. super();
  7. }
  8. public Integer getId() {
  9. return id;
  10. }
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. }
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;
	}

}

 

User.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2many;
  2. import java.util.Set;
  3. public class User {
  4. private Integer id;
  5. private String name;
  6. private Set<Role> roles;
  7. public User() {
  8. super();
  9. }
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public Set<Role> getRoles() {
  23. return roles;
  24. }
  25. public void setRoles(Set<Role> roles) {
  26. this.roles = roles;
  27. }
  28. }
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;
	}
}

 

二:映射文件:

Role.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.many2many">
  5. <class name="Role" table="t_role">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native">
  8. </generator>
  9. </id>
  10. <property name="name" column="name" />
  11. </class>
  12. </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>

 

User.hbm.xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="com.tao.hibernate.many2many"
  5. auto-import="false">
  6. <class name="User" table="t_user">
  7. <id name="id" column="id" type="integer">
  8. <generator class="native">
  9. </generator>
  10. </id>
  11. <property name="name" column="name" />
  12. <set name="roles" table="t_user_role" cascade="all">
  13. <key column="userId" />
  14. <many-to-many class="Role" column="roleId" />
  15. </set>
  16. </class>
  17. </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>

 

三:测试类:

Testmain.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2many;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import org.hibernate.Session;
  6. import org.hibernate.Transaction;
  7. import org.junit.Test;
  8. import com.tao.hibernate.HibernateUtils;
  9. public class TestMain {
  10. Session session = HibernateUtils.getFactory().openSession();
  11. @Test
  12. public void testSave() {
  13. User u1 = new User();
  14. u1.setName("u1");
  15. User u2 = new User();
  16. u2.setName("u2");
  17. User u3 = new User();
  18. u3.setName("u3");
  19. Role r1 = new Role();
  20. r1.setName("admin");
  21. Role r2 = new Role();
  22. r2.setName("guest");
  23. Set<Role> roles1 = new HashSet<Role>();
  24. roles1.add(r1);
  25. Set<Role> roles2 = new HashSet<Role>();
  26. roles2.add(r2);
  27. Set<Role> roles3 = new HashSet<Role>();
  28. roles3.add(r1);
  29. roles3.add(r2);
  30. u1.setRoles(roles1);
  31. u2.setRoles(roles2);
  32. u3.setRoles(roles3);
  33. Transaction ts = session.beginTransaction();
  34. ts.begin();
  35. session.save(u1);
  36. session.save(u2);
  37. session.save(u3);
  38. ts.commit();
  39. }
  40. @Test
  41. public void testLoad() {
  42. Session session = HibernateUtils.getFactory().openSession();
  43. User u1 = (User) session.load(User.class, 1);
  44. System.out.println(u1.getName());
  45. for (Iterator<Role> iter = u1.getRoles().iterator(); iter.hasNext();) {
  46. System.out.println(iter.next().getName());
  47. }
  48. }
  49. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值