1

thank a lot for view my question.

I have a 2 Entities with relationship: one-to-many. They are "staff" and "role".(One Staff has many Role)

This is "Role" in Staff :

@OneToMany(cascade = CascadeType.ALL, mappedBy = "staffId", fetch = FetchType.EAGER) private Collection<Role> roleCollection;

And I tried to select a specify numbers of Staff with codes:

Criteria crit = session.createCriteria(Staff.class);
crit.setFirstResult(0);
crit.setMaxResults(20);
crit.setFetchMode("id", FetchMode.SELECT); // This solution in SOF but it not work
crit.addOrder(Order.asc("id"));
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
result = crit.list();

I known my problem. - Criteria.DISTINCT_ROOT_ENTITY remove duplicate entity after getting data from Database - But setMaxResults(20); calculate number of results include duplicate result.

So, my List of result less than my maxResult()

I tried some solutions was solved on SOF, but all of them failed. I think because my config in entity class: FetchType.EAGER but i dont want to change it to LAZY

can you help me to solve it?

William
  • 862
  • 7
  • 14
  • 1
    Use Projections.distinct. Look at this: http://stackoverflow.com/questions/25536868/criteria-distinct-root-entity-vs-projections-distinct – Sampisa Nov 01 '16 at 14:35
  • Thank @Sampisa for your support. I'm a newbie in Hibernate. I tried with your solution. But if i use `Projections.distinct(Projections.property("id"));` It change my result collection to a Collection of Integer. I cant find any good way to transfer it back to entity. – William Nov 01 '16 at 15:17
  • 1
    Did you try this? http://stackoverflow.com/questions/300491/how-to-get-distinct-results-in-hibernate-with-joins-and-row-based-limiting-pagi – Sampisa Nov 01 '16 at 16:25
  • Yes, It exactly i tried. And it convert Result in `criteria.list` to a list of Integer. :'( Something wrong here – William Nov 01 '16 at 16:39
  • Here's my error code: `java.lang.ClassCastException: java.lang.Integer cannot be cast to model.entity.Staff` – William Nov 01 '16 at 16:43
  • Sorry for not reading carefully your question. There is nothing you should do: entities that you get Staff are *obviously* distinct: you can use "distinct" only if you have - clearly - equal "values" to aggregate. – Sampisa Nov 03 '16 at 15:34
  • 1
    [continues] With your Criteria crit you ask to search items ordered by "id" from class Staff; primary keys are different, so entities are different. If you instead want to get just few attributes, you must use Projections.distinct, so that results where selected properties are equals are collapsed in just one entity. If I've not understood your point, please clarify posting entities and example of data, and what you expect as result from your query. Thanks – Sampisa Nov 03 '16 at 15:44
  • 1
    Thank you so much Sampina. My mistake was on `crit.setFetchMode("id", FetchMode.SELECT); ` - i fixed my issue by change property "id" to "roleCollection" which column be joined . Before time, i didn't understand exactly about Fetch and Fetch mode. Thank you so much again Sampina. – William Nov 04 '16 at 03:18

0 Answers0