Hibernate: getting too many rows
Asked Answered
V

4

5

I have problem with getting rows from my database using 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. 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:

@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. This is this class header:

@MappedSuperclass
public abstract class MyEntity<T extends Serializable>

What am I doing wrong? Is this problem with EmbeddedId?

EDIT #1 As I have realized this is problem with this:

@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. In the result I am getting rows amount * 10. The problem is probably with Hibernate annotation in my entities.

Verso answered 9/10, 2012 at 8:32 Comment(3)
Are the returned Objects all the same instance? Meaning do you get back 20 of the same exact Object references?Laminated
@SteveEbersole : I suppose yes. I will check that tomorrow because at this moment I can't. I checked out the sql from Hibernate console and I have realized that Hibernate are getting from database few another tabels. These tables are connected by foreing keys of course. But I don't know why for simple getting all rows from one table my code is getting few attached tables. And for one row in the database I am getting ten rows as a query result.Verso
@SteveEbersole : I have checked that. I am getting 10 instances for one Object and 10 for another.Verso
R
7

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:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Rafiq answered 12/10, 2012 at 22:23 Comment(0)
C
2

@Embeddable only means that MyPK 's columns will be columns in the My class. Your problem might be @ManyToOne @JoinColumn(name = "property") since it's the same with "property" in MyPK

Crossing answered 9/10, 2012 at 9:5 Comment(1)
Yes, it's the same with "property" in MyPK. I have written it in this way because this is part of primary key and also it is a foreign key in this table.Verso
A
1

You can set the maximum number of results by setting this on the criteria with this method: setMaxResults(int maxResults)

Accelerant answered 9/10, 2012 at 8:54 Comment(3)
Yes, I can, But I need more complex solution.Verso
Why would you want a complex solution when there is a simple one?!Accelerant
Because I won't know how many rows I want to have returned. This count is stored in database and may vary.Verso
L
1

Primary key classes need to define equals() and hashCode(), in terms of the aggregated values (qwerty and property, here). Most likely when process the ResultSet, Hibernate is not seeing the entity keys across multiple rows as equal.

From Section 2.4 of the JPA 2.0 specification (in case it helps):

The primary key class must define equals and hashCode methods. 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

Laminated answered 9/10, 2012 at 13:44 Comment(2)
I have defined equals() and hashCode() but my issue is still the same. Do you have another suggestions?Verso
I wonder if my entities are correct so I have also asked this question: https://mcmap.net/q/2029983/-is-this-correct-better-object-relational-mapping/845220 Could you read also this one?Verso

© 2022 - 2024 — McMap. All rights reserved.