Hibernate returning List with NULL values

出处:http://www.coderanch.com/t/217438/ORM/java/Hibernate-returning-List-NULL-values

...

Problem description:

I have a one-to-many relationship. As i retrieve the Parent object, hibernate returns the Parent object and List of Child objects. The problem is, the List contains some null values so there's some error when iterating through it.

Any idea?

Thanks in advance!

Best sollution:

Hi all! 


Since I also ran into this problem and since it does not seem to be solved yet, I registered with the forum to share my experiences. 

At first it is to say that Sandeep Vaid is right with his explanation, but some of you misinterpreted his answer. In fact the number of elements in the resulting java list does not directly depend on the number of records in the database. By using the <list>-element in a mapping you tell hibernate that you want to get a list with indexed containers. You know, Java lists can be accessed by using the get()-method and an index. The sub-element <list-index> tells Hibernate which column stores the information where to put the retrieved records in the resulting java list. Let's assume you have the following table: 

Children 

child_id name parent_id birthOrder age 
1 ____ Sepp ___ 2 _____ 0 _____ 5 
2 ____ Maria ___ 2 _____ 1 _____ 9 
3 ____ Hans ___ 2 _____ 2 _____ 11 

We can reuse the mapping of Giuseppe for the my example: 

  1. <list name="children" lazy="false">  
  2.   <key column="parent_id"/>  
  3.   <list-index column="birthOrder"/>  
  4.   <one-to-many class="org.family.model.Child" />  
  5. </list>  


Since Hibernate assumes by default that you start counting at zero this mapping gives us exactly what we expect: 
Java-List: [Sepp, Maria, Hans] 

If you start the  birthOrder with 1 you run into our problem. But for that case Giuseppe's solution is absolutely suitable (as long as the numbering has no "holes"). 
So what if you do not have the column  birthOrder but only the column  age? Of course you can also use this one for your <list-index>-element. Hibernate will then assume that you want a list that has Sepp on position 5, Maria on 9 and Hans on 11. The resulting gaps in the list will be filled with null values: 
[null, null, null, null, null, Sepp, null, null, null, Maria, null, Hans] 

You may use the base-attribute to eliminate the leading null-values, but the most you can get is: 
[Sepp, null, null, null, Maria, null, Hans] 

The proper solution is simple. Do not use the <list>-element, but use a <bag>. Bags are not ordered and may contain duplicates, but they also map to the java interface List and you are able to let Hibernate sort them by using the order-by-attribute. 
At last the example to my solution: 


  1. <bag name="children" lazy="false" order-by="age asc">  
  2.   <key column="parent_id"/>  
  3.   <one-to-many class="org.family.model.Child"/>  
  4. </bag>  


Kind regards
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值