czy11421 wrote:
Two questions
No. 1
I have one-to-many List set as Lazy. In search, I found if I use findById and detached creita will load List automatically, if I use find (HQL), it will not load the List.
Anybody knows why ? If I want a strict "Lazy", in any scenario, don't load the List, what should I do ?
No.2
If an object has a List which contains 10 elements, some of them are NOT changed, then we don't need to do UPDATE for performance. but I realize that Hibernate will not do this way, how could we improve it ?
Thanks.
For No.1, I am not sure about the Criteria API since I rarely use it but in HQL, to the best of my understanding, if your query starts with from then the configuration is taken into account. however, if your statement start with a select then the query specifies what needs to be fetched.
For No.2, this is the default behaviour but you need to have the colleciton in memory so that hibernate can detect which ones are modified. In many case the collection is coming from outside and it is not in the L1 cache. If you like to get around this, you need to enable a second level cache to avoid performance disasters, and then you need to fetch the collection before replacing it with the new one. Hibernate will take care of the rest. One another point, for class you can instruct hibernate to do a select before update. This might be also possible for collection. And ah, don't forget that caching a collection is good enough. You need to enable caching on the entities that might be inside your collection otherwise hibernate will do a good couple of selects to get those.
Farzad-