java Hibernate 查询多次给出相同的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10564832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:35:23  来源:igfitidea点击:

Hibernate query gives same record multiple times

javahibernate

提问by Muhammad Salman Farooq

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

我正在 Eclipse 中进行休眠。我正在执行简单的“来自”查询。这是代码

  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

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

enter image description here

在此处输入图片说明

here is what i want

这是我想要的

enter image description here

在此处输入图片说明

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.

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

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

请在这方面帮助我,因为我是休眠的新手

回答by shridharama

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.

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

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.

要么将您的 id 元素更改为指向一个 PK 列,要么使用复合 id 元素,以防您的表只有一个复合主键。

回答by Eildosa

Are you sure that the table is correctly filled? try :

你确定表格填对了吗?尝试 :

List list = sess1.createQuery("SELECT * FROM Myview").list();

futhermore, you are getting this list from a view? are you sure that you made this view correctly?

此外,您是从视图中获取此列表吗?你确定你正确地制作了这个视图吗?

回答by user1112699

You should use the distinct keyword to filter the same result.

您应该使用 distinct 关键字来过滤相同的结果。

回答by martin.hatas

Your entity Myview have to implement java.io.Serializable interface

您的实体 Myview 必须实现 java.io.Serializable 接口

回答by Koray Tugay

Put the objects returned by Hibernate to a LinkedHashSet and return the LinkedHashSet.

把Hibernate返回的对象放到一个LinkedHashSet中,返回LinkedHashSet。

回答by Saif

If you have association in the mapping then check if fetch=FetchType.EAGER. If yes then use other fetch type or fetchMode.

如果您在映射中有关联,请检查 fetch=FetchType.EAGER. 如果是,则使用其他获取类型或 fetchMode。