Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate returns only one result (After changing table names)

I am using hibernate retrieve results from my MySQL database into my Java project. Recently, I had a lot of redundant data and had to manually clean up the database by copying the required data into new tables and then renaming the newly created table to old table.

But, now querying the database with hibernate gives only one row as the result. I have manually checked the database and there are several different rows in the database. My query to Hibernate is something like this:

Criteria c = session.createCriteria(UserDto.class);
c.setMaxResults(100);
List<UserDto> users = c.list();

users contains 100 elements but all are the same.

The mapping of userDto is here.

Any idea what is happening here?

like image 483
Pulkit Goyal Avatar asked Dec 08 '11 21:12

Pulkit Goyal


1 Answers

If your UserDto class has ToMany relations, then this is quite possible that outer join on them results in many records which all contain one and the same user data. You should use

session.createCriteria(UserDto.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
like image 196
Nikem Avatar answered Oct 28 '22 19:10

Nikem