Hibernate查询多次提供相同的记录 [英] Hibernate query gives same record multiple times
问题描述
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查询多次提供相同的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!