HiberNate学习-2映射关系

 1.Hibernate的一对一主键关联映射

 

默认cascade属性为true;

 

核心代码:

1.主键生成策略:

Xml代码 复制代码 收藏代码
  1. <!-- Person的主键来源于idCard, 也就是共享idCard主键 -->
  2. <generatorclass="foreign">
  3. <paramname="property">idCard</param>
  4. </generator>
<!-- Person的主键来源于idCard, 也就是共享idCard主键 -->
<generator class="foreign">
	<param name="property">idCard</param>
</generator>

2.one-to-one配置

Java代码 复制代码 收藏代码
  1. <!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载
  2. constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard
  3. -->
  4. <one-to-one name="idCard" constrained="true" />
<!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载
	constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard
 -->
<one-to-one name="idCard" constrained="true" />

 

 

一:实体类:

Person.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one;
  2. public class Person {
  3. private Integer id;
  4. private String name;
  5. private IdCard idCard;
  6. public Person() {
  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 IdCard getIdCard() {
  22. return idCard;
  23. }
  24. public void setIdCard(IdCard idCard) {
  25. this.idCard = idCard;
  26. }
  27. }
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;
	}

}

 

Idcard.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one;
  2. public class IdCard {
  3. private Integer id;
  4. private String cardNo;
  5. private Person person;
  6. public IdCard() {
  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 getCardNo() {
  16. return cardNo;
  17. }
  18. public void setCardNo(String cardNo) {
  19. this.cardNo = cardNo;
  20. }
  21. public Person getPerson() {
  22. return person;
  23. }
  24. public void setPerson(Person person) {
  25. this.person = person;
  26. }
  27. }
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;
	}

}

二:映射文件:

Person.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.one2one">
  5. <class name="Person" table="t_Person">
  6. <id name="id" column="id" type="integer">
  7. <!-- Person的主键来源于idCard, 也就是共享idCard主键 -->
  8. <generator class="foreign">
  9. <param name="property">idCard</param>
  10. </generator>
  11. </id>
  12. <property name="name" column="name" />
  13. <!-- one-to-one标签的含义,指示hibernate怎样加载它的关联对象,默认根据主键加载
  14. constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idcard
  15. -->
  16. <one-to-one name="idCard" constrained="true" />
  17. </class>
  18. </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>

 

Idcard.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.one2one">
  5. <class name="IdCard" table="t_IdCard">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="cardNo" column="cardNo" />
  10. <one-to-one name="person" fetch="select"/>
  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.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>

三:测试类

Java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import org.junit.Test;
  5. import com.tao.hibernate.HibernateUtils;
  6. publicclass TestMain {
  7. @Test
  8. publicvoid testSave() {
  9. Session session = HibernateUtils.getFactory().openSession();
  10. IdCard idCard = new IdCard();
  11. idCard.setCardNo("100");
  12. Person person = new Person();
  13. person.setName("Tao");
  14. person.setIdCard(idCard);
  15. Transaction ts = session.beginTransaction();
  16. ts.begin();
  17. session.save(person);
  18. ts.commit();
  19. }
  20. @Test
  21. publicvoid testLoad() {
  22. Session session = HibernateUtils.getFactory().openSession();
  23. Person person = (Person) session.load(Person.class, 2);
  24. System.out.println(person.getName());
  25. System.out.println(person.getIdCard().getCardNo());
  26. }
  27. @Test
  28. publicvoid testLoad2() {
  29. Session session = HibernateUtils.getFactory().openSession();
  30. IdCard idCard = (IdCard) session.load(IdCard.class, 2);
  31. System.out.println(idCard.getCardNo());
  32. System.out.println(idCard.getPerson().getName());
  33. }
  34. }

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">

 

一:实体类:

Idcard2.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one2;
  2. public class IdCard2 {
  3. private Integer id;
  4. private String cardNo;
  5. private Person2 person;
  6. public IdCard2() {
  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 getCardNo() {
  16. return cardNo;
  17. }
  18. public void setCardNo(String cardNo) {
  19. this.cardNo = cardNo;
  20. }
  21. public Person2 getPerson() {
  22. return person;
  23. }
  24. public void setPerson(Person2 person) {
  25. this.person = person;
  26. }
  27. }
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;
	}

}

 

Person2.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one2;
  2. public class Person2 {
  3. private Integer id;
  4. private String name;
  5. private IdCard2 idCard;
  6. public Person2() {
  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 IdCard2 getIdCard() {
  22. return idCard;
  23. }
  24. public void setIdCard(IdCard2 idCard2) {
  25. this.idCard = idCard2;
  26. }
  27. }
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;
	}

}

 

二:映射文件:

Idcard2.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.one2one2">
  5. <class name="IdCard2" table="t_IdCard2">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native" />
  8. </id>
  9. <property name="cardNo" column="cardNo" />
  10. <one-to-one name="person" property-ref="idCard" />
  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.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>

 

Person2.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.one2one2">
  5. <class name="Person2" table="t_Person2">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native">
  8. </generator>
  9. </id>
  10. <property name="name" column="name" />
  11. <many-to-one name="idCard" unique="true"/>
  12. </class>
  13. </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>

 

三:测试类:

Java代码 复制代码 收藏代码
  1. package com.tao.hibernate.one2one2;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import org.junit.Test;
  5. import com.tao.hibernate.HibernateUtils;
  6. publicclass TestMain {
  7. @Test
  8. publicvoid testSave() {
  9. Session session = HibernateUtils.getFactory().openSession();
  10. IdCard2 idCard = new IdCard2();
  11. idCard.setCardNo("100");
  12. Person2 person = new Person2();
  13. person.setName("Tao");
  14. person.setIdCard(idCard);
  15. Transaction ts = session.beginTransaction();
  16. ts.begin();
  17. session.save(idCard);
  18. session.save(person);
  19. ts.commit();
  20. }
  21. @Test
  22. publicvoid testLoad() {
  23. Session session = HibernateUtils.getFactory().openSession();
  24. Person2 person = (Person2) session.load(Person2.class, 1);
  25. //System.out.println(person.getName());
  26. System.out.println(person.getIdCard().getCardNo());
  27. }
  28. @Test
  29. publicvoid testLoad2() {
  30. Session session = HibernateUtils.getFactory().openSession();
  31. IdCard2 idCard = (IdCard2) session.load(IdCard2.class, 1);
  32. System.out.println(idCard.getCardNo());
  33. System.out.println(idCard.getPerson().getName());
  34. }
  35. }  

 

3  hibernate多对一关联映射

关联映射的本质:
* 将关联关系映射到数据库,所谓的关联关系是对象模型在内存中的一个或多个引用

<many-to-one>会在多的一端加入一个外键,指向一的一端,这个外键是由<many-to-one>
中的column属性定义的,如果忽略了这个属性那么默认的外键与实体的属性一致

<many-to-one>标签的定义示例:
* <many-to-one name="group" column="groupid"/>

理解级联的含义?
* 是对象的连锁操作

 

一:实体类

Group.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2one;
  2. public class Group {
  3. private Integer id;
  4. private String name;
  5. public Group() {
  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.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;
	}

}

 

User.java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2one;
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private Group group;
  6. public User() {
  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 Group getGroup() {
  22. return group;
  23. }
  24. public void setGroup(Group group) {
  25. this.group = group;
  26. }
  27. }
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;
	}

}

 

二:映射文件

Group.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.many2one">
  5. <class name="Group" table="t_group">
  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.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>

 

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.many2one">
  5. <class name="User" table="t_user">
  6. <id name="id" column="id" type="integer">
  7. <generator class="native">
  8. </generator>
  9. </id>
  10. <property name="name" column="name" />
  11. <many-to-one name="group" column="groupId"/>
  12. </class>
  13. </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>

 

三:测试类:

Testmain .java代码 复制代码 收藏代码
  1. package com.tao.hibernate.many2one;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import org.junit.Test;
  5. import com.tao.hibernate.HibernateUtils;
  6. public class TestMain {
  7. Session session = HibernateUtils.getFactory().openSession();
  8. @Test
  9. public void testSave() {
  10. Group group = new Group();
  11. group.setName("second group");
  12. User user = new User();
  13. user.setName("Tao2");
  14. user.setGroup(group);
  15. Transaction ts = session.beginTransaction();
  16. ts.begin();
  17. session.save(group);
  18. session.save(user);
  19. ts.commit();
  20. }
  21. @Test
  22. public void testLoad() {
  23. Session session = HibernateUtils.getFactory().openSession();
  24. User user = (User) session.load(User.class, 1);
  25. System.out.println(user.getName());
  26. System.out.println(user.getGroup().getName());
  27. }
  28. }

你可能感兴趣的:(Hibernate)