0

For optimization purposes I would like to update some entities with an hibernate stateless session.

These entities, however, are loaded from a "classical" session (I need cache feature to load my entities quicker). All of this could be good but some of my entities are lazily initialized, so their class name is something like : myentity_$$_javassist_22. Thus, my stateless session does not want to update my entity.

Is there a way to transform a lazily initialized entity to a loaded entity ?

thank you

Fabien Henon
  • 783
  • 15
  • 37

1 Answers1

2

If you manually fetch all the lazy-loaded entities before giving them to your stateless session it will work (no more proxies). But it could cost a lot as it would generate many queries to load the full object graph.

Either you fetch them before with Hibernate.initialize(lazyEntity) for instance, either you kill them with proxy-killing methods (but you will save null: probably it is not what you want).

Check this other question about Hibernate stateless session to see if it is a good solution for your problem Using StatelessSession for Batch processing

Community
  • 1
  • 1
zenbeni
  • 7,019
  • 3
  • 29
  • 60
  • I fetch them using left join fetch in my queries but some of them are still lazily-loaded (maybe because of the cache). Beside, I know my entities (the lazily-loaded ones) are in cache because when I access them, no more query is made. So, I don't know how to fetch them as I'm doing everything to fetch them. That's why I'm wondering if it was possible to manually load them to be able to use them with my stateless session. By the way, I need a stateless session bcause batching will make me clear the cache and I need the cache for the next steps of my application – Fabien Henon Sep 03 '13 at 14:23
  • Hibernate.initialize(object) is a way to force the load of a lazy entity. – zenbeni Sep 03 '13 at 15:43
  • But I think it is probably better to fully load your graph in one query (logically better performance). You can eventually use a native query or the worker API of Hibernate to define a custom SQL query for that. – zenbeni Sep 03 '13 at 15:49
  • That's already what I'm doing (fully load in one query), but because of the cache, some of the children are not fully loaded (I get proxy classes). That's why I was looking for a solution to manually load them (no query is executed as the entity is in the cache, so it's fast). Hibernate.initialize(object) is the solution indeed, it works for me and my code is 12 times faster. I'll mark your answer as the correct one and edit it to add a link that helped me with Hibernate.initialize() (because this function alone is not enought) – Fabien Henon Sep 03 '13 at 21:23