package nl.one_to_one; import java.io.Serializable; import org.apache.commons.lang.builder.ToStringBuilder; /** * @hibernate.class * table="passport" * */ public class Passport implements Serializable { /** identifier field */ private Integer id; /** nullable persistent field */ private String area; /** nullable persistent field */ private nl.one_to_one.User user; /** full constructor */ public Passport(Integer id, String area, nl.one_to_one.User user) { this.id = id; this.area = area; this.user = user; } /** default constructor */ public Passport() { } /** minimal constructor */ public Passport(Integer id) { this.id = id; } /** * @hibernate.id * generator-class="assigned" * type="java.lang.Integer" * column="id" * */ public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } /** * @hibernate.property * column="area" * length="100" * */ public String getArea() { return this.area; } public void setArea(String area) { this.area = area; } /** * @hibernate.one-to-one * outer-join="auto" * */ public nl.one_to_one.User getUser() { return this.user; } public void setUser(nl.one_to_one.User user) { this.user = user; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } }
package nl.one_to_one; import java.io.Serializable; import org.apache.commons.lang.builder.ToStringBuilder; /** * @hibernate.class * table="user" * */ public class User implements Serializable { /** identifier field */ private Integer id; /** nullable persistent field */ private String name; /** nullable persistent field */ private String pwd; /** nullable persistent field */ private nl.one_to_one.Passport passport; /** full constructor */ public User(Integer id, String name, String pwd, nl.one_to_one.Passport passport) { this.id = id; this.name = name; this.pwd = pwd; this.passport = passport; } /** default constructor */ public User() { } /** minimal constructor */ public User(Integer id) { this.id = id; } /** * @hibernate.id * generator-class="assigned" * type="java.lang.Integer" * column="id" * */ public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } /** * @hibernate.property * column="name" * length="100" * */ public String getName() { return this.name; } public void setName(String name) { this.name = name; } /** * @hibernate.property * column="pwd" * length="100" * */ public String getPwd() { return this.pwd; } public void setPwd(String pwd) { this.pwd = pwd; } /** * @hibernate.one-to-one * class="nl.one_to_one.Passport" * outer-join="auto" * constrained="true" * */ public nl.one_to_one.Passport getPassport() { return this.passport; } public void setPassport(nl.one_to_one.Passport passport) { this.passport = passport; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } }
passport.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="nl.one_to_one.Passport" table="passport" > <meta attribute="class-description" inherit="false"> @hibernate.class table="passport" </meta> <id name="id" type="java.lang.Integer" column="id" > <meta attribute="field-description"> @hibernate.id generator-class="assigned" type="java.lang.Integer" column="id" </meta> <generator class="native" /> </id> <property name="area" type="java.lang.String" column="area" length="100" > <meta attribute="field-description"> @hibernate.property column="area" length="100" </meta> </property> <!-- Associations --> <!-- bi-directional one-to-one association to User --> <one-to-one name="user" class="nl.one_to_one.User" outer-join="auto" cascade="all" > <meta attribute="field-description"> @hibernate.one-to-one outer-join="auto" </meta> </one-to-one> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <!-- Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/ --> <class name="nl.one_to_one.User" table="user" > <meta attribute="class-description" inherit="false"> @hibernate.class table="user" </meta> <id name="id" type="java.lang.Integer" column="id" > <meta attribute="field-description"> @hibernate.id generator-class="assigned" type="java.lang.Integer" column="id" </meta> <generator class="foreign"> <param name="property">passport</param> </generator> </id> <property name="name" type="java.lang.String" column="name" length="100" > <meta attribute="field-description"> @hibernate.property column="name" length="100" </meta> </property> <property name="pwd" type="java.lang.String" column="pwd" length="100" > <meta attribute="field-description"> @hibernate.property column="pwd" length="100" </meta> </property> <!-- Associations --> <!-- bi-directional one-to-one association to Passport --> <one-to-one name="passport" class="nl.one_to_one.Passport" outer-join="auto" constrained="true" > <meta attribute="field-description"> @hibernate.one-to-one class="nl.one_to_one.Passport" outer-join="auto" constrained="true" </meta> </one-to-one> </class> </hibernate-mapping>
测试代码:
package test; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; import nl.one_to_one.Passport; import nl.one_to_one.User; public class One2One extends TestCase { protected void tearDown() throws Exception { super.tearDown(); seesion.close(); } Session seesion = null; public void init() { System.out.println("test......."); } protected void setUp() throws Exception { super.setUp(); Configuration configuration = new Configuration().configure(); SessionFactory factory = configuration.buildSessionFactory(); seesion = factory.openSession(); } public void testItem() { try { Passport passport=new Passport(); passport.setArea("zhangzhou"); User user=new User(); user.setName("cyhgo"); user.setPwd("123456"); passport.setUser(user); user.setPassport(passport); Transaction transaction=seesion.beginTransaction(); seesion.save(passport); seesion.flush(); transaction.commit(); List list=seesion.createQuery("from Passport").list(); Iterator<Passport> it=list.iterator(); System.out.println(list.size()); while(it.hasNext()) { Passport pass=it.next(); System.out.println(pass.getArea()); User user2=pass.getUser(); System.out.println(user2.getName()+"_"+user2.getPwd()); } } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
passport 的ID设置为"自动增长"
user的ID设置为外键,关联于passport的ID
在测试的时候必须要注意这两张表之间因为已经存在了一对一的关系,所以我们不能只写
user.setPassport(passport);
而忽略了
passport.setUser(user);
如果不写cascade="all"或者写成cascade="none"的话,即使你写了
user.setPassport(passport);
passport.setUser(user);
也不会发生任何事情,只有passport会被存储。