org.hibernate.HibernateException:找到了多个具有给定标识符的行

时间:2016-12-28 22:01:55

标签: java postgresql hibernate hql

org.hibernate.HibernateException:找到了多个具有给定标识符的行:578,用于类:com.hibernate.query.performance.persistence.model.Store

数据库没有重复的存储行,标识符为578.使用SQL检查:

SELECT * 
FROM store
WHERE store.store_id = 578;

它返回了0条记录。

关于OneTwo的其他问题指出问题可能与OneToOne映射本身有关。 Store和Staff实体具有 OneToOne 关系,我的映射如下:

员工:

@OneToOne(mappedBy = "manager_staff", cascade = CascadeType.ALL, orphanRemoval = true)
    public Store getStore() {
        return store;
    }

商品

@OneToOne
    @JoinColumn(name = "manager_staff_id", referencedColumnName = "staff_id", nullable = false)
    public Staff getManager_staff() {
        return manager_staff;
    }

如何解决?

更新

当查询被修改为:

时,异常发生了一些变化
Query query = session.createQuery("select c " +
                        " from Rental r, Customer c join fetch c.address address join fetch address.store store join fetch address.city city " +
                        " where r.customer = c " +
                        " and r.returnDate is null");

例外:

org.hibernate.HibernateException: More than one row with the given identifier was found: 2951, for class: com.hibernate.query.performance.persistence.model.Store

没有对数据库的修改。我不确定HQL是否正确,因为JProfiler无法捕获任何JPA / Hibernate记录。捕获的唯一指标是运行的JDBC连接。

1 个答案:

答案 0 :(得分:-1)

您的数据有问题,OneToOne表示您必须拥有该链接 - 因此请确保查询返回一条记录。如果它需要是可选的,你必须使它OneToMany