简体   繁体   English

使用@OneToMany关系时,即使数据库中只有一个对象,SELECT也会返回多个对象-Java,Hibernate,PostgreSQL

[英]When the @OneToMany relation is used, SELECT returns multiple objects even if there is only one object in the database - Java, Hibernate, PostgreSQL

I have two tables in my database: 1. Warehouse 2. WarehouseItem 我的数据库中有两个表:1.仓库2. WarehouseItem

Relation between them are like listed below: 它们之间的关系如下所示:

@Entity
@Table(name = "warehouse")
public class WarehouseModel {

     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="warehouse")
     private List<WarehouseItemModel> _items;

and

@Entity
@Table(name = "warehouseItem")
public class WarehouseItemModel {
    @ManyToOne
    public WarehouseModel warehouse;

and now I want to SELECT all the objects of the entity WarehouseModel: 现在我要选择实体WarehouseModel的所有对象:

public List getObjects(Class pClass)
    {
        startTime();
        connect();
        Session session = getSession();
        Transaction lTransaction = session.beginTransaction();
        List lRet = session.createCriteria(pClass).list();
        lTransaction.commit();
        endTime("getObjects: " + lRet.size() + " objects");
        Collections.reverse(lRet);
        return lRet;
    }

In my database I have: 在我的数据库中,我有:

  • 1x object in the table: Warehouse (WarehouseModel.java) 表中的1x对象:仓库 (WarehouseModel.java)
  • 5x objects in the table: WarehouseItem (WarehouseItemModel.java) 表中的5个对象:WarehouseItem (WarehouseItemModel.java)

When I want to retrive all the Warehouses including related WarehouseItems: 当我想检索包括相关WarehouseItems在内的所有仓库时:

databaseConnector.eDocumentConnector.getObjects(WarehouseModel.class)

the result is: 结果是:

- 5x the same object of WarehouseModel -5x WarehouseModel的相同对象

It seems that there is dependancy that I always get as much entities of the same WarehouseModel as there is WarehouseItemModels inside field WarehouseModel._items 似乎存在依赖关系,即我总是获得与同一WarehouseModel相同的实体,与字段WarehouseModel._items中的WarehouseItemModels一样多。

How to fix it and why it happens? 如何修复它以及为什么会发生? (I have more relations like this one in my project and if it happends here, maybe it happends also in the other places) (我的项目中有更多类似的关系,如果在这里发生,也许在其他地方也发生)

Project details: - Java 1.8 - Hibernate 5.0.7 - database: PostgreSQL 9.5.2 项目详细信息:-Java 1.8-Hibernate 5.0.7-数据库:PostgreSQL 9.5.2

通过在休眠查询中使用DISTINCT可以解决此问题:

session.createCriteria(pClass).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); 

More importantly, you should understand how a 1-to-many relationship works in SQL. 更重要的是,您应该了解SQL中的一对多关系是如何工作的。

The raw select statement is going to be something like 原始的select语句将类似于

SELECT
    .
    .
    <what columns you want>
    .
FROM
    warehouse w
       JOIN warehouseItem wi ON (w.id = wi.warehouse_id)
WHERE
    <whatever you need to filter on>

When you join, you're always going to get the number of rows that are related to the primary table's ID. 加入后,您将始终获得与主表的ID相关的行数。 Since there are 5 rows in the child table, 5 rows are returned, where the warehouse columns are duplicated each time but the warehouseItem columns are unique to each row. 由于子表中有5行,因此将返回5行,每次仓库列都重复一次,但是WarehouseItem列对于每一行都是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM