private int id;
private String name;
private IdCard idCard;
private int id;
private String cardNo;
private Person person;
<hibernate-mapping>
<class name="" table="t_person">
<id name="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name"/>
<one-to-one name="idCard" constrained="true"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="" table="t_idcard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"/>
<one-to-one name="person"/>
</class>
</hibernate-mapping>
public class est extends TestCase {
public void testSave1() {
Session session = null;
try {
session = ();
();
IdCard idCard = new IdCard();
("333333333333");
Person person = new Person();
("飞飞");
(idCard);
//不会出现TransientObjectException异常
//因为一对一主键关联映射中,默认了cascade属性
(person);
().commit();
}catch(Exception e) {
();
().rollback();
}finally {
(session);
}
}
public void testLoad1() {
Session session = null;
try {
session = ();
sess
Hibernate-一对一主键关联双向 来自淘豆网m.daumloan.com转载请标明出处.