简体   繁体   English

Hibernate:行太多了

[英]Hibernate: getting too many rows

I have problem with getting rows from my database using Hibernate. 我在使用Hibernate从数据库中获取行时遇到问题。 When I would like to get only one row, I am receiving 20. When I would like to get all of rows from table with about 1.5k rows, I am receiving exactly 15.2k rows. 当我想只获得一行时,我收到的是20.当我想从表中获得大约1.5k行的所有行时,我收到的行数恰好为15.2k。 Entity class of this table has composite primary key. 该表的实体类具有复合主键。

This is my code for getting all rows: 这是获取所有行的代码:

Criteria criteria = getSession().createCriteria(type);
criteria.setCacheable(true).setCacheRegion(BaseEntity.PACKAGE);
criteria.list();

And this is my Entity class: 这是我的实体类:

@javax.persistence.Entity
@Table(name = "my_table")
public class My extends MyEntity<MyPK> {

    @EmbeddedId
    private MyPK id;

    @Column(name = "text", nullable = false)
    protected String text;

    @ManyToOne
    @JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
    protected Option option;

    @Override
    public MyPK getId() {
        return id;
    }

    @Override
    public void setId(MyPK id) {
        this.id = id;
    }

    //getters and setter
}

And this is MyPK class: 这是MyPK类:

@Embeddable
public class MyPK implements Serializable {

   @Column(name = "qwerty")
   protected String qwerty;

   @Column(name = "property")
   protected String property;

   //constructors, getters and setters
}

MyEntity class is abstract class with @MappedSuperclass annotation. MyEntity类是带有@MappedSuperclass注释的抽象类。 This is this class header: 这是这个类标题:

@MappedSuperclass
public abstract class MyEntity<T extends Serializable>

What am I doing wrong? 我究竟做错了什么? Is this problem with EmbeddedId ? 这是EmbeddedId问题吗?

EDIT #1 As I have realized this is problem with this: 编辑#1我已经意识到这是问题所在:

@ManyToOne
@JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
protected Option option;

This object contains foreign key to another table. 此对象包含另一个表的外键。 And this another table has reference to another. 另一张表引用了另一张表。 And this last table has 10 rows for previous table. 最后一个表有10行用于上一个表。 In the result I am getting rows amount * 10. The problem is probably with Hibernate annotation in my entities. 在结果中我得到行数量* 10.问题可能在于我的实体中的Hibernate注释。

It looks like you're probably eagerly joining a many-to-one relationship somewhere. 看起来你可能正急切地想要在某个地方加入多对一的关系。 The default behavior is that you get one entity for each row returned by the database. 默认行为是您为数据库返回的每一行获取一个实体。 If you don't want to change the eager fetching, but do want to remove duplicates in your result, you need to use this ResultTransformer : 如果您不想更改ResultTransformer获取的提取,但确实想要删除结果中的重复项,则需要使用此ResultTransformer

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

@Embeddable only means that MyPK 's columns will be columns in the My class. @Embeddable仅表示MyPK的列将是My类中的列。 Your problem might be @ManyToOne @JoinColumn(name = "property") since it's the same with "property " in MyPK 您的问题可能是@ManyToOne @JoinColumn(name = "property")因为它与MyPK中的“property ”相同

您可以通过此方法在条件上设置此结果来设置最大结果数: setMaxResults(int maxResults)

Primary key classes need to define equals() and hashCode(), in terms of the aggregated values ( qwerty and property , here). 主键类需要根据聚合值( qwertyproperty ,这里)定义equals()和hashCode()。 Most likely when process the ResultSet, Hibernate is not seeing the entity keys across multiple rows as equal. 很可能在处理ResultSet时,Hibernate没有看到跨越多行的实体键是相等的。

From Section 2.4 of the JPA 2.0 specification (in case it helps): 从JPA 2.0规范的2.4节(如果有帮助):

The primary key class must define equals and hashCode methods. 主键类必须定义equals和hashCode方法。 The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped 这些方法的值相等的语义必须与键映射到的数据库类型的数据库相等一致

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

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