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());
- }
- }