1.Hibernate的一对一主键关联映射
默认cascade属性为true;
核心代码:
1.主键生成策略:
- <!-- Person的主键来源于idCard, 也就是共享idCard主键 -->
- <generatorclass="foreign">
- <paramname="property">idCard</param>
- </generator>
<!-- Person的主键来源于idCard, 也就是共享idCard主键 --> <generator class="foreign"> <param name="property">idCard</param> </generator>
2.one-to-one配置
- <!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载
- constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard
- -->
- <one-to-one name="idCard" constrained="true" />
<!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载 constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard --> <one-to-one name="idCard" constrained="true" />
一:实体类:
- package com.tao.hibernate.one2one;
- public class Person {
- private Integer id;
- private String name;
- private IdCard idCard;
- public Person() {
- 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 IdCard getIdCard() {
- return idCard;
- }
- public void setIdCard(IdCard idCard) {
- this.idCard = idCard;
- }
- }
package com.tao.hibernate.one2one; public class Person { private Integer id; private String name; private IdCard idCard; public Person() { 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 IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
- package com.tao.hibernate.one2one;
- public class IdCard {
- private Integer id;
- private String cardNo;
- private Person person;
- public IdCard() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getCardNo() {
- return cardNo;
- }
- public void setCardNo(String cardNo) {
- this.cardNo = cardNo;
- }
- public Person getPerson() {
- return person;
- }
- public void setPerson(Person person) {
- this.person = person;
- }
- }
package com.tao.hibernate.one2one; public class IdCard { private Integer id; private String cardNo; private Person person; public IdCard() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
二:映射文件:
- <?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.one2one">
- <class name="Person" table="t_Person">
- <id name="id" column="id" type="integer">
- <!-- Person的主键来源于idCard, 也就是共享idCard主键 -->
- <generator class="foreign">
- <param name="property">idCard</param>
- </generator>
- </id>
- <property name="name" column="name" />
- <!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载
- constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard
- -->
- <one-to-one name="idCard" constrained="true" />
- </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.one2one"> <class name="Person" table="t_Person"> <id name="id" column="id" type="integer"> <!-- Person的主键来源于idCard, 也就是共享idCard主键 --> <generator class="foreign"> <param name="property">idCard</param> </generator> </id> <property name="name" column="name" /> <!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载 constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard --> <one-to-one name="idCard" constrained="true" /> </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.one2one">
- <class name="IdCard" table="t_IdCard">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="cardNo" column="cardNo" />
- <one-to-one name="person" fetch="select"/>
- </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.one2one"> <class name="IdCard" table="t_IdCard"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="cardNo" column="cardNo" /> <one-to-one name="person" fetch="select"/> </class> </hibernate-mapping>
三:测试类
- package com.tao.hibernate.one2one;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import com.tao.hibernate.HibernateUtils;
- publicclass TestMain {
- @Test
- publicvoid testSave() {
- Session session = HibernateUtils.getFactory().openSession();
- IdCard idCard = new IdCard();
- idCard.setCardNo("100");
- Person person = new Person();
- person.setName("Tao");
- person.setIdCard(idCard);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(person);
- ts.commit();
- }
- @Test
- publicvoid testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- Person person = (Person) session.load(Person.class, 2);
- System.out.println(person.getName());
- System.out.println(person.getIdCard().getCardNo());
- }
- @Test
- publicvoid testLoad2() {
- Session session = HibernateUtils.getFactory().openSession();
- IdCard idCard = (IdCard) session.load(IdCard.class, 2);
- System.out.println(idCard.getCardNo());
- System.out.println(idCard.getPerson().getName());
- }
- }
2 Hibernate的一对一唯一外键关联映射
一对一唯一外键关联映射实际上是多对一关联映射的特例。
可以采用many-to-one标签,指定多的一端的unique=true,这样就限制了多的一端的多重性唯一。
通过这种手段映射一对一唯一外键关联。
<many-to-one name="idCard" unique="true"/>
一对一唯一外键关联双向,需要在另一端(idCard),添加<one-to-one>标签,指示hibernate如何加载其关联对象,默认根据主键加载,外键关联映射中,因为两个实体采用的是person外键维护的关系,所以不能指定主键加载person,而要根据person的外键加载,所以采用如下映射方式:
<one-to-one name="person" property-ref="idCard">
一:实体类:
- package com.tao.hibernate.one2one2;
- public class IdCard2 {
- private Integer id;
- private String cardNo;
- private Person2 person;
- public IdCard2() {
- super();
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getCardNo() {
- return cardNo;
- }
- public void setCardNo(String cardNo) {
- this.cardNo = cardNo;
- }
- public Person2 getPerson() {
- return person;
- }
- public void setPerson(Person2 person) {
- this.person = person;
- }
- }
package com.tao.hibernate.one2one2;
public class IdCard2 {
private Integer id;
private String cardNo;
private Person2 person;
public IdCard2() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public Person2 getPerson() {
return person;
}
public void setPerson(Person2 person) {
this.person = person;
}
}
- package com.tao.hibernate.one2one2;
- public class Person2 {
- private Integer id;
- private String name;
- private IdCard2 idCard;
- public Person2() {
- 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 IdCard2 getIdCard() {
- return idCard;
- }
- public void setIdCard(IdCard2 idCard2) {
- this.idCard = idCard2;
- }
- }
package com.tao.hibernate.one2one2;
public class Person2 {
private Integer id;
private String name;
private IdCard2 idCard;
public Person2() {
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 IdCard2 getIdCard() {
return idCard;
}
public void setIdCard(IdCard2 idCard2) {
this.idCard = idCard2;
}
}
二:映射文件:
- <?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.one2one2">
- <class name="IdCard2" table="t_IdCard2">
- <id name="id" column="id" type="integer">
- <generator class="native" />
- </id>
- <property name="cardNo" column="cardNo" />
- <one-to-one name="person" property-ref="idCard" />
- </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.one2one2">
<class name="IdCard2" table="t_IdCard2">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="cardNo" column="cardNo" />
<one-to-one name="person" property-ref="idCard" />
</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.one2one2">
- <class name="Person2" table="t_Person2">
- <id name="id" column="id" type="integer">
- <generator class="native">
- </generator>
- </id>
- <property name="name" column="name" />
- <many-to-one name="idCard" unique="true"/>
- </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.one2one2">
<class name="Person2" table="t_Person2">
<id name="id" column="id" type="integer">
<generator class="native">
</generator>
</id>
<property name="name" column="name" />
<many-to-one name="idCard" unique="true"/>
</class>
</hibernate-mapping>
三:测试类:
- package com.tao.hibernate.one2one2;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.junit.Test;
- import com.tao.hibernate.HibernateUtils;
- publicclass TestMain {
- @Test
- publicvoid testSave() {
- Session session = HibernateUtils.getFactory().openSession();
- IdCard2 idCard = new IdCard2();
- idCard.setCardNo("100");
- Person2 person = new Person2();
- person.setName("Tao");
- person.setIdCard(idCard);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(idCard);
- session.save(person);
- ts.commit();
- }
- @Test
- publicvoid testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- Person2 person = (Person2) session.load(Person2.class, 1);
- //System.out.println(person.getName());
- System.out.println(person.getIdCard().getCardNo());
- }
- @Test
- publicvoid testLoad2() {
- Session session = HibernateUtils.getFactory().openSession();
- IdCard2 idCard = (IdCard2) session.load(IdCard2.class, 1);
- System.out.println(idCard.getCardNo());
- System.out.println(idCard.getPerson().getName());
- }
- }
3 hibernate多对一关联映射
关联映射的本质:
* 将关联关系映射到数据库,所谓的关联关系是对象模型在内存中的一个或多个引用
<many-to-one>会在多的一端加入一个外键,指向一的一端,这个外键是由<many-to-one>
中的column属性定义的,如果忽略了这个属性那么默认的外键与实体的属性一致
<many-to-one>标签的定义示例:
* <many-to-one name="group" column="groupid"/>
理解级联的含义?
* 是对象的连锁操作
一:实体类
- package com.tao.hibernate.many2one;
- public class Group {
- private Integer id;
- private String name;
- public Group() {
- 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.many2one;
public class Group {
private Integer id;
private String name;
public Group() {
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.many2one;
- public class User {
- private Integer id;
- private String name;
- private Group group;
- 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 Group getGroup() {
- return group;
- }
- public void setGroup(Group group) {
- this.group = group;
- }
- }
package com.tao.hibernate.many2one;
public class User {
private Integer id;
private String name;
private Group group;
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 Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
二:映射文件
- <?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.many2one">
- <class name="Group" table="t_group">
- <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.many2one">
<class name="Group" table="t_group">
<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.many2one">
- <class name="User" table="t_user">
- <id name="id" column="id" type="integer">
- <generator class="native">
- </generator>
- </id>
- <property name="name" column="name" />
- <many-to-one name="group" column="groupId"/>
- </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.many2one">
<class name="User" table="t_user">
<id name="id" column="id" type="integer">
<generator class="native">
</generator>
</id>
<property name="name" column="name" />
<many-to-one name="group" column="groupId"/>
</class>
</hibernate-mapping>
三:测试类:
- package com.tao.hibernate.many2one;
- 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() {
- Group group = new Group();
- group.setName("second group");
- User user = new User();
- user.setName("Tao2");
- user.setGroup(group);
- Transaction ts = session.beginTransaction();
- ts.begin();
- session.save(group);
- session.save(user);
- ts.commit();
- }
- @Test
- public void testLoad() {
- Session session = HibernateUtils.getFactory().openSession();
- User user = (User) session.load(User.class, 1);
- System.out.println(user.getName());
- System.out.println(user.getGroup().getName());
- }
- }
相关推荐
Hibernate是Java平台上的一款开源对象关系映射(ORM)框架,它允许开发者用面向对象的方式处理数据库操作。在SSH(Spring、Struts、Hibernate)框架中,Hibernate作为持久层的解决方案,极大地简化了数据库操作的...
Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发人员在处理数据库时使用面向对象的概念,极大地简化了数据库操作。此版本包含了Hibernate的核心库和其他相关组件,使得开发者能够将Java类与数据库表...
2. `org.hibernate.annotations.common`:这个包包含了通用的注解处理工具,如`AnnotationConfiguration`和`AnnotationBinder`,它们负责解析和绑定注解信息。 3. `org.hibernate.cfg`:这里的类主要用于配置和初始...
《HibernateTools 3.2.4 Beta1:高效数据库管理和对象关系映射工具》 HibernateTools是Java开发者的重要辅助工具,它为Hibernate框架提供了一系列实用的集成开发环境(IDE)插件和命令行工具,极大地简化了数据操作...
《精通Hibernate》学习-对象关系映射基础 Hibernate是一个开源的对象关系映射(ORM)框架,它为Java开发者提供了一种方便地将数据库操作转换为面向对象的方式。在这个压缩包中,我们很可能会找到关于Hibernate基础...
Hibernate 是一个著名的开源对象关系映射(ORM)框架...综上所述,"hibernate3-2 官方jar包+api"是学习和开发基于Hibernate 3.2的Java应用的基础资源,涵盖了ORM的核心概念和技术,帮助开发者更高效地管理数据库操作。
首先,我们来看一下“一对多”(One-to-Many)的映射关系。在Hibernate中,这种关系通常用在一个实体可以拥有多个子实体的情况,如一个部门有多个员工。`hibernate_one2many_1`和`hibernate_one2many_2`的示例可能...
4. **建立映射文件或使用注解**: Hibernate使用XML映射文件(.hbm.xml)或注解来定义对象与表之间的映射关系。注解方式更简洁,可以直接在实体类上定义。 5. **操作数据**: 通过Session对象进行数据库操作。增删...
本资料集"Accp6.0 Y2 Hibernate1-2章上机练习和课后代码"是针对Accp6.0课程第二年(Y2)关于Hibernate框架的深入学习,提供了个人编写的代码实践案例。由于项目规模较大,被拆分为三个部分,这里主要讨论的是前两章...
- 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,作为Java领域最著名的对象关系映射(ORM)框架之一,极大地简化了数据库操作,使得开发者能够用面向对象的方式来处理数据,而无需关注底层的SQL语句。`hibernate-core-1a8aca9.ta`是Hibernate的核心组件...
Hibernate实体关联关系映射--学习总结.pdf
这些注解使得开发者可以更加直观地定义实体类及其与数据库表的映射关系,简化了传统的Hibernate XML配置文件。 **二、主要注解** 1. `@Entity`: 用于标记一个类为数据库中的实体类,对应于一张数据库表。 2. `@...
### Hibernate—我的Hibernate学习笔记 #### 一、对象持久化的理论与实践 ##### 对象持久化概念 对象持久化指的是将内存中的对象状态保存至外部持久性存储设备(如硬盘),以便于日后需要时能够恢复其状态。这一...
同时,深入学习这些映射关系,不仅可以让我们更好地理解Hibernate的设计思想,还可以提升我们对他人代码的理解能力和代码质量。总之,系统学习和实践Hibernate实体关联映射是每一位Java开发者必备的技能。
Hibernate ORM(Object-Relational Mapping)是Java领域中最受欢迎的持久化框架之一,它提供了一种便捷的方式将Java对象映射到关系数据库,从而实现了对象-关系的解耦。"hibernate-orm-master.zip"文件包含了...