Many returned records call stackoverflow with Hibernate
If there are many return records from the database. He will face stackoverflow issue.
User
is a class that has one to many relationships (to 3 other classes). When I print out the SQL, I found that the system runs the same query many times to get the data from the DB. Does anyone know what the problem is?
result.addAll(getCurrentSession().createCriteria(User.class)
.add(Restrictions.ilike("name", "tom", MatchMode.ANYWHERE))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list());
If you are looking for users with tom in the name only then it should not run out of memory if you have a lot of% volume% hehe.
You can try limiting it so that it only returns the ones that you can handle at a time.
I'm not sure if anyone can figure out why it is running the same request without further information.
source to share
Are you using EntityMode.DOM4J by accident? (i.e. Hibernate -> XML instead of Hibernate -> POJO). Nested relationships don't work out of tune because the XML generator recurses endlessly where POJOS handles them as best it can.
I can clarify, but if you are not using DOM4J, then the problem is different.
source to share
This will help you know exactly what duplicate queries you see. As others have pointed out, we are just speculating without seeing your comparisons. You might be looking forward to Hibernate. Here Hibernate tries to load the entire object graph for each user (and their addresses, phone numbers, pets, etc.) matching the most common name containing the query "tom". Try disabling default loading for custom properties in Hibernate files or annotations, then follow these steps:
Let's say you get a lot of duplicate table lookups USER_ADDRESS (hibernate property "addresses")
, USER_PHONE (property "phones")
and USER_PET (property "pets")
. Use the following criteria to combine these attributes into the original query and reduce duplicate queries. Hibernate knows how to split these columns into separate entities. You can also try adding the maximum number of results returned.
Criteria myUsers = getCurrentSession().createCriteria(User.class);
myUsers = myUsers.add(Restrictions.ilike("name", "tom", MatchMode.ANYWHERE));
myUsers.createCriteria("addresses"); // NEW
myUsers.createCriteria("phones"); // NEW
myUsers.createCriteria("pets"); // NEW
myUsers.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
myUsers.setMaxResults(100); // NEW
result.addAll(myUsers.list());
You can read more in sections 15.4-15.6 here
source to share