本教程每节课都附带源码,强烈大家建议配合源码学习。
本节源码:http://download.csdn.net/detail/e421083458/5253726
组件映射
对象关系:一个对象是另外一个对象的一部分
实现方式两种:
Annotaion方式:
对象模型
Husband(id,name,wife)
Wife(name,age)
Annotation:
在Husband的wife属性上建立注解
@Embedded 表明该对象是从别的位置嵌入过来的,是不需要单独映射的表.
这种方式生成的表为husband(id,name,wifename,wifeage),不会生成wife表.
@AttributeOverride注解需要写在getWife方法上,可以重新指定生成的Wife类组件生成的字段名,例如:Husband与Wife两个类中都有name字段,这样在生成表的时候会有冲突,此时采用@AttributeOverride注解可以指定Wife类中的name属性对应新的字段名—“wifename”,不过@AttributeOverride注解不常用,因为有更好的解决方法.
1:不要在组件的两个映射类中写同名属性;2:如果真的有重复,那么可以在分类中(此处为Wife类)的重复名称的属性上使用如下内容以指定新的字段名:
@Column(name="wifename")
public String getName() {
return name;
}
另外,@ Embeddable注解好像是写在分类(Wife类)的类名前的,不过好像不写也行
@Embeddable
public class Wife {… …}
XML方式:
<class name="Husband" >
<id name="id">
<generator class="native"/>
</id>
<property name="name"></property>
<component name="wife">
<property name="wifename"/>
<property name="wifeAge"/>
</component>
</class>
项目源码:
Husband.java
package com.bjsxt.hibernate; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Embedded public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; } }
Wife.java
package com.bjsxt.hibernate; public class Wife { private String wifename; private int age; public String getWifename() { return wifename; } public void setWifename(String wifename) { this.wifename = wifename; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Husband.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Husband" > <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <component name="wife"> <property name="wifename"></property> <property name="age"></property> </component> </class> </hibernate-mapping>
测试脚本:
package com.bjsxt.hiberante; import java.util.Date; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.Session; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.bjsxt.hibernate.Husband; import com.bjsxt.hibernate.Wife; public class HibernateQLTest { private static SessionFactory sf; @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("BeforeClass"); sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("AfterClass"); sf.close(); } // @Test public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); } @Test public void testOneTOOne_Wife(){ Session session = sf.getCurrentSession(); session.beginTransaction(); Wife wife = new Wife(); wife.setWifename("wife"); wife.setAge(24); Husband husband = new Husband(); husband.setName("husband"); husband.setWife(wife); session.save(husband); //再保存丈夫 session.getTransaction().commit(); } public static void main(String args[]){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sf.getCurrentSession(); session.beginTransaction(); Wife wife = new Wife(); wife.setWifename("wife"); wife.setAge(24); Husband husband = new Husband(); husband.setName("husband"); husband.setWife(wife); session.save(husband); //再保存丈夫 session.getTransaction().commit(); sf.close(); } }