You can get the linked collection in transaction to load it while you're still within the transaction.
You can get the linked collection in transaction to load it while you're still within the transaction: User user = sessionFactory. GetCurrentSession(). Get(User.
Class, userId); user. GetLinkedCollection().size(); return user; As BalusC has pointed out, you can use initialize() instead of size(). That's a lot cleaner.
Then when you return such an entity, the lazy field is already initialized. Replying to your PS - is using transactions on service level (rather than DAO) level feasible? It seems to be, as doing each DAO call in separate transaction seems to be a waste (and may be incorrect).
Konrad Garus Please see my postscript of the question, here the text is less readable, so I ask you there. – EugeneP Mar 15 '10 at 12:32 @EugeneP See updated answer. – Konrad Garus Mar 15 '10 at 13:15.
You need to access it in the same If this is not doable, then you need to bernate#initialize() the collection during query. Also see chapter 19.1.4 of the Update: since you're using Spring, I bet that you're using a web application. In that case, I'd recommend to use the "open session in view" approach with Spring's OpenSessionInViewFilter.
BalusC Please see my updated question (pps) – EugeneP Mar 15 '10 at 14:37 Transactions lies outside DAO control. Transactions are to be controlled by your service/business layer. You normally start a transaction and then call one or more DAO methods (which may be related to each other that way that you'd like to rollback everything whenever a step fails) and then commit the transaction.In webapplications it's an acceptable practice to start the transaction in the beginning of the HTTP request and commit it when the HTTP response is been sent.
The DAO just have to get the connection from the currently available/open transaction. – BalusC Mar 15 '10 at 14:43.
I find that it's best to put @Transactional at the service layer, rather than the DAO layer. Otherwise, all your DAO calls are in separate hibernate sessions - all that object equality stuff won't work.
In my opinion best way to solve this problem will be to design application in a session-per-request model. Then, if you even have an object taken from DAO, until your OSIV pattern works you can use the object safely anywhere in application, even in views without bothering this stuff. This is probably better solution that those proposed because: initialize() or size is a very artificial workaround - what if you want to have User with different collection initialized, would you write another method for getting user?
Service layer transactional model is OK, but the same problem comes when you want to get object extracted from the service layer to use it in controller or view.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.