一对一关联映射(采用唯一外键+单向)





一对一关联映射(采用唯一外键+单向)

hibernate一对一唯一外键关联映射(单向关联Person---->IdCard)

一对唯一外键关联映射是多对一关联映射的特例

可以采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多的一端的多重性为一
通过这种手段映射一对一唯一外键关联



第一步:首先建立实体关系类:如下:
person类的实体关系:
public class Person { private int id; private String name; private IdCard idCard; //在Person类中持有IdCard的引用 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; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }

IdCord类的实体关系类如下:
public class IdCard { private int id; private String cardNo; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } }

第二步:加入实体类的关系映射:
IdCard类的实体映射文件如下:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.IdCard" table="t_idcard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"/>
</class>
</hibernate-mapping>
Person类的实体映射文件如下:
<hibernate-mapping> <class name="com.bjsxt.hibernate.Person" table="t_person"> <id name="id"> <generator class="native"/>//这里的策略为自增。 </id> <property name="name"/> <many-to-one name="idCard" unique="true"/>//在这个属性里要加入unique属性,因为为一对一关系映射。这样就限制了他的唯一性 </class> </hibernate-mapping>


第三步:加入Hibernate的配置文件:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2one_ufk_1</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjsxt</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/bjsxt/hibernate/IdCard.hbm.xml"/>
<mapping resource="com/bjsxt/hibernate/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


第四步:写测试代码:
public class One2OneTest extends TestCase { public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("88888888888888"); Person person = new Person(); person.setName("菜10"); person.setIdCard(idCard); //不能成功保存,因为IdCard是Transient状态,必须先保存IdCard或者设置为cascade session.save(person); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testSave2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("88888888888888"); session.save(idCard);//保存了之后就没问题了 Person person = new Person(); person.setName("菜10"); person.setIdCard(idCard); session.save(person); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Person person = (Person)session.load(Person.class, 2); System.out.println("person.name=" + person.getName()); System.out.println("idCard.cardNo=" + person.getIdCard().getCardNo()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }















































你可能感兴趣的:(关联映射)