好好学习,天天向上!

Hibernate映射一对一关联(之二)

使用one-to-many和many-to-one配置一对一双向关联:

其实,就是把上面的两种综合起来配置:

在Person.hbm.xml中配置:

<many-to-one name="card"
            class="org.shirdrn.entity.Card"
            update="false"
            insert="false"
            cascade="save-update">
            <column name="id" length="50" not-null="true" unique="true" />
</many-to-one>

在card.hbm.xml中配置:

<set name="persons"
        inverse="true"
        cascade="save-update">
            <key>
                <column name="id" length="50" not-null="true" unique="true" />
            </key>
            <one-to-many class="org.shirdrn.entity.Person" />
</set>

这样,上面的测试程序都可以实现级联存储更新。

 

使用one-to-one配置一对一单向关联:

在Person里面配置与Card的一对一单向关联,配置如下:

<one-to-one name="card"
            cascade="save-update"
            class="org.shirdrn.entity.Card">
</one-to-one>

测试程序如下:

Session session = HibernateSessionFactory.getSession();
   Transaction tx = null;
   try{
    tx = session.beginTransaction();
    Person p = new Person();
    p.setId("222403199901011119");
    p.setName("刘备");
    Card c = new Card();
    c.setId("222403199901011119");
    c.setCardNo("JL-2008-03-04-0001");
    c.setReleaseTime(new Date());
    c.setReleaseArea("长春");
    c.setEffectTime(new Integer(20));
    p.setCard(c);
    session.save(p);
    tx.commit();
   }
   catch(Exception e){
    tx.rollback();
    e.printStackTrace();
   }
   finally{
    HibernateSessionFactory.closeSession();
   }

可以实现级联保存:

Hibernate: select card_.id, card_.cardNo as cardNo3_, card_.releaseTime as releaseT3_3_, card_.releaseArea as releaseA4_3_, card_.effectTime as effectTime3_ from hibernate.dbo.card card_ where card_.id=?
Hibernate: insert into hibernate.dbo.person (name, gender, age, addr, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernate.dbo.card (cardNo, releaseTime, releaseArea, effectTime, id) values (?, ?, ?, ?, ?)

同理,也可以在Card里面配置与Person的一对一单向关联,实现同理。

 

总结一下一对一双向关联的实现,有很多中配置方式:

1、一方:

<one-to-one name="card"
            cascade="save-update"
            class="org.shirdrn.entity.Card">
</one-to-one>

另一方:

<one-to-one name="person"
            cascade="save-update"
            class="org.shirdrn.entity.Person">
</one-to-one>

或者

<set name="persons"
        inverse="true"
        cascade="save-update">
            <key>
                <column name="id" length="50" not-null="true" unique="true" />
            </key>
            <one-to-many class="org.shirdrn.entity.Person" />
</set>

或者

<many-to-one name="person"
            class="org.shirdrn.entity.Person"
            update="false"
            insert="false"
            cascade="save-update">
            <column name="id" length="50" not-null="true" unique="true" />
</many-to-one>

2、一方:

<many-to-one name="card"
            class="org.shirdrn.entity.Card"
            update="false"
            insert="false"
            cascade="save-update">
            <column name="id" length="50" not-null="true" unique="true" />
</many-to-one>

另一方:

<many-to-one name="person"
            class="org.shirdrn.entity.Person"
            update="false"
            insert="false"
            cascade="save-update">
            <column name="id" length="50" not-null="true" unique="true" />
</many-to-one>

这点很好理解,因为unique="true",就相当于一方是one-to-one,另一方也是one-to-one。

3、一方:

<set name="cards"
        inverse="true"
        cascade="save-update">
            <key>
                <column name="id" length="50" not-null="true" unique="true" />
            </key>
            <one-to-many class="org.shirdrn.entity.Card" />
</set>

另一方:

<set name="persons"
        inverse="true"
        cascade="save-update">
            <key>
                <column name="id" length="50" not-null="true" unique="true" />
            </key>
            <one-to-many class="org.shirdrn.entity.Person" />
</set>

这种配置方式过于麻烦,没有人会在配置双向一对一关联的时候这样配置。

posted @ 2013-01-11 11:14  忍性而为  阅读(150)  评论(0编辑  收藏  举报
好好学习,天天向上!