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());
- }
- }
- }
相关推荐
Hibernate是Java平台上的一款开源对象关系映射(ORM)框架,它允许开发者用面向对象的方式处理数据库操作。在SSH(Spring、Struts、Hibernate)框架中,Hibernate作为持久层的解决方案,极大地简化了数据库操作的...
Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发人员在处理数据库时使用面向对象的概念,极大地简化了数据库操作。此版本包含了Hibernate的核心库和其他相关组件,使得开发者能够将Java类与数据库表...
3. `org.hibernate.cfg`:这里的类主要用于配置和初始化Hibernate环境,如`AnnotationConfiguration`类就是配置Hibernate的入口,它会读取注解并生成相应的映射信息。 4. `org.hibernate.boot.model`和`org....
《HibernateTools 3.2.4 Beta1:高效数据库管理和对象关系映射工具》 HibernateTools是Java开发者的重要辅助工具,它为Hibernate框架提供了一系列实用的集成开发环境(IDE)插件和命令行工具,极大地简化了数据操作...
《精通Hibernate》学习-对象关系映射基础 Hibernate是一个开源的对象关系映射(ORM)框架,它为Java开发者提供了一种方便地将数据库操作转换为面向对象的方式。在这个压缩包中,我们很可能会找到关于Hibernate基础...
4. **建立映射文件或使用注解**: Hibernate使用XML映射文件(.hbm.xml)或注解来定义对象与表之间的映射关系。注解方式更简洁,可以直接在实体类上定义。 5. **操作数据**: 通过Session对象进行数据库操作。增删...
首先,我们来看一下“一对多”(One-to-Many)的映射关系。在Hibernate中,这种关系通常用在一个实体可以拥有多个子实体的情况,如一个部门有多个员工。`hibernate_one2many_1`和`hibernate_one2many_2`的示例可能...
- Object-Relational Mapping:通过XML或注解方式定义实体类和数据库表之间的映射关系,实现对象与数据的自动转换。 3. Hibernate 5.0.7.Final新特性 - 支持JSR-349 Bean Validation 1.1:增强了实体验证功能,...
"hibernate-release-4.2.13.Final.tgz"是一个全面的Hibernate学习资源,不仅提供了运行环境所需的jar包,还包含了丰富的实践示例和详尽的文档。无论你是初学者还是经验丰富的开发者,都可以从中受益,进一步提升在...
Hibernate 是一个著名的开源对象关系映射(ORM)框架,它为Java开发者提供了便捷的数据持久化方式,使得在Java应用程序中操作数据库变得更加简单。这个“hibernate3-2 官方jar包+api”包含了 Hibernate 3.2 版本的...
Hibernate,作为Java领域最著名的对象关系映射(ORM)框架之一,极大地简化了数据库操作,使得开发者能够用面向对象的方式来处理数据,而无需关注底层的SQL语句。`hibernate-core-1a8aca9.ta`是Hibernate的核心组件...
这些注解使得开发者可以更加直观地定义实体类及其与数据库表的映射关系,简化了传统的Hibernate XML配置文件。 **二、主要注解** 1. `@Entity`: 用于标记一个类为数据库中的实体类,对应于一张数据库表。 2. `@...
Hibernate实体关联关系映射--学习总结.pdf
《韩顺平老师 Hibernate3-3 教学源码详解》 Hibernate,作为一个开源的对象关系映射(ORM)框架,是Java开发中的重要工具。它为开发者提供了在Java应用程序中操作数据库的强大支持,使得开发者无需关注底层SQL的...
同时,深入学习这些映射关系,不仅可以让我们更好地理解Hibernate的设计思想,还可以提升我们对他人代码的理解能力和代码质量。总之,系统学习和实践Hibernate实体关联映射是每一位Java开发者必备的技能。
### Hibernate—我的Hibernate学习笔记 #### 一、对象持久化的理论与实践 ##### 对象持久化概念 对象持久化指的是将内存中的对象状态保存至外部持久性存储设备(如硬盘),以便于日后需要时能够恢复其状态。这一...
Hibernate ORM(Object-Relational Mapping)是Java领域中最受欢迎的持久化框架之一,它提供了一种便捷的方式将Java对象映射到关系数据库,从而实现了对象-关系的解耦。"hibernate-orm-master.zip"文件包含了...