Hibernate: getting too many rows

久未见 提交于 2019-12-01 06:36:17

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);

@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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!