两种不同方式的一对一映射关系:
1)配置文件:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/testdb</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/my/hbm/Employer.hbm.xml"/> <mapping resource="com/my/hbm/Address.hbm.xml"/> <mapping resource="com/my/hbm/Detial.hbm.xml"/> </session-factory> </hibernate-configuration>
Employer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.my.bean.Employer" table="employer"> <id name="id" type="long"> <column name="id"></column> <generator class="native"></generator> </id> <property name="companyName" type="java.lang.String" length="100" column="company_name" not-null="true" /> <property name="createTime" type="java.util.Date"> <column name="create_time" not-null="true"/> </property> <one-to-one name="address"></one-to-one> <one-to-one name="detial" property-ref="employer"></one-to-one> </class> </hibernate-mapping>
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.my.bean.Address" table="address"> <id name="id" type="long" column="id"> <generator class="foreign"> <param name="property">employer</param> </generator> </id> <property name="location" column="location" length="200" not-null="true" type="java.lang.String"></property> <one-to-one name="employer" constrained="true"></one-to-one> </class> </hibernate-mapping>
Detial.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.my.bean.Detial" table="detial"> <id name="id" type="long" column="id"> <generator class="native"></generator> </id> <many-to-one name="employer" column="employer_id" unique="true"></many-to-one> <property name="info" column="info" type="java.lang.String" length="3000"></property> </class> </hibernate-mapping>
2)Java bean:
package com.my.bean; import java.util.Date; public class Employer { private long id; private String companyName; private Date createTime; private Address address; private Detial detial; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Detial getDetial() { return detial; } public void setDetial(Detial detial) { this.detial = detial; } }
package com.my.bean; public class Address { private long id; private String location; private Employer employer; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Employer getEmployer() { return employer; } public void setEmployer(Employer employer) { this.employer = employer; } }
package com.my.bean; public class Detial { private long id; private String info; private Employer employer; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public Employer getEmployer() { return employer; } public void setEmployer(Employer employer) { this.employer = employer; } }
3)测试:
package com.my.init; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.xml.soap.Detail; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.my.bean.Address; import com.my.bean.Detial; import com.my.bean.Employer; import com.my.bean.User; import com.my.bean.UserAccount; import com.my.dao.util.HibernateUtil; public class Test { @SuppressWarnings("unchecked") public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); // new employer Employer employer = new Employer(); employer.setCompanyName("my company"); employer.setCreateTime(new Date()); // new address Address address = new Address(); address.setLocation("china"); // new detail Detial detail = new Detial(); detail.setInfo("info"); // add relation employer.setAddress(address); employer.setDetial(detail); address.setEmployer(employer); detail.setEmployer(employer); session.save(employer); session.save(address); session.save(detail); // Get employer String hsqlSelect = "from Employer"; Query query = session.createQuery(hsqlSelect); List<Employer> es = query.list(); if(es != null && es.size() > 0){ Employer e = es.get(0); System.out.println(e.getCompanyName()); System.out.println(e.getDetial().getInfo()); } tx.commit(); session.close(); } }
关注点:
1)Employer.hbm.xml
<generator class="native"></generator>
这个generator的class="native"值有好几种,具体使用方法可参见:http://blog.csdn.net/ye1992/article/details/19632001
<one-to-one name="address"></one-to-one> <one-to-one name="detial" property-ref="employer"></one-to-one>
这里有两个one-to-one,两种一对一的不同映射方式。
其中第二种one-to-one中有一个属性引用,对应的是在detial中的employer属性。
property-ref的使用说明可参见:http://blog.csdn.net/fengyuanfa/article/details/5096764
2)Address.hbm.xml
<id name="id" type="long" column="id"> <generator class="foreign"> <param name="property">employer</param> </generator> </id>
这是声明一个外部键字段的一对一关系用法。
<one-to-one name="employer" constrained="true"></one-to-one>
这里这个contrained设为true是必需的。
3)Detial.hbm.xml
<many-to-one name="employer" column="employer_id" unique="true"></many-to-one>
这是一对一关系的另一种现实方式,使用的是many-to-one,但unique设为true是必需的。
这两种不同的一对一关系映射,参考资料:
http://blog.csdn.net/mr__fang/article/details/8592000
http://www.tutorialspoint.com/hibernate/hibernate_one_to_one_mapping.htm