hibernate 单向one-to-many association(1)

单向one-to-many association 比较的简单,在parent端进行定制xml

   <set name="items" table="item" lazy="false" inverse="false" cascade="all">
         
         <key column="categry_id"/>
         <one-to-many class="hibernate.Item" />
    </set>

值得注意的是name,要与categary类中的属性名一致,java.util.Set item; 关键是getItems()名字必须一致。

   Categry c2= (Categry)session.get(Categry.class, new Integer(7));
     Set items= c2.getItems();
     Iterator it=items.iterator();
     while(it.hasNext())
     {
      Item i1=(Item)it.next();
      if(i1!=null)
      {
        System.out.println(" Item name is "+i1.getName()+ "--item categry name is "+ i1.getCategry().getName());
      
      }
      else
       System.out.println(" error ");
     }

 

Hibernate: select categry0_.categry_id as categry1_0_0_, categry0_.name as name0_0_, categry0_.description as descript3_0_0_ from categry categry0_ where categry0_.categry_id=?
Hibernate: select items0_.categry_id as categry3_1_, items0_.item_id as item1_1_, items0_.item_id as item1_1_0_, items0_.name as name1_0_, items0_.categry_id as categry3_1_0_ from item items0_ where items0_.categry_id=?

2次select , the first from categary and the other from item table

更正:因为错误的配置,被俺弄成了双向one-to-many association,  改成单向后上面的代码i1.getCategry().getName(), 会报null pointer 错误,因为单向的association, parent 没有去维护child中对parent的reference.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值