Hibernate查询多次提供相同的记录

编程入门 行业动态 更新时间:2024-10-12 01:24:24
本文介绍了Hibernate查询多次提供相同的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在研究eclipse中的hibernate。我正在执行简单的'From'查询。这里是代码

List list = sess1.createQuery(From Myview)。 System.out.println(找到的记录:+ list.size()); 迭代器< Myview> i = list.iterator(); while(i.hasNext()) { Myview nS = i.next(); System.out.println(nS.getFirstName()+ - + nS.getLastName()+ - + nS.getAddressLine1()); $ / code>

问题是list.size()返回11,这是正确的我在桌上有11条记录。 但是当我在while循环中时,多次显示相同的记录,循环在第11次迭代后终止。这里是我的输出

这就是我想要的

现在您可以看到,在我的输出中,记录显示了11次,但同一记录再次重复然后再次。我需要的是输出显示在后面的图像。

请在这方面帮助我,因为我是hibernate新手

<当您的HBM文件中的ID元素不是数据库表中的PK时,会发生这种情况。 Hibernate将具有相同ID的所有行视为同一个对象。

或者将您的id元素更改为指向PK列,或者在您的表只包含复合主键时使用composite-id元素。

I am working on hibernate in eclipse. I am executing simple 'From' query. Here is the code

List list = sess1.createQuery("From Myview").list(); System.out.println("Records Found :"+list.size()); Iterator<Myview> i = list.iterator(); while(i.hasNext()) { Myview nS = i.next(); System.out.println(nS.getFirstName()+" -- "+nS.getLastName()+" -- "+nS.getAddressLine1()); }

The problem is the list.size() returns 11, which is right as i have 11 records in my table. But when i am in while loop, the same records shown multiple times and the loop termintes after 11th iteration. here is my output

here is what i want

Now you can see that in my output, record is displayed 11 times but the same record is repeated again and again. And what i need is the output displayed in the later image.

Kindly help me in this regard, as i am new to hibernate

解决方案

This happens when the id element in your hbm file is not a PK in your DB table. Hibernate treats all rows with the same ID as the same object.

Either change your id element to point to a PK column or use the composite-id element in case your table only has a composite primary key.

更多推荐

Hibernate查询多次提供相同的记录

本文发布于:2023-11-26 23:53:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!