When calling a remote service (e.g. over RMI) to load a list of entities from a database using Hibernate, how do you manage it to initialize all the fields and references the client needs?
Example: The client calls a remote method to load all customers. With each customer the client wants the reference to the customer's list of bought articles to be initialized.
I can imagine the following solutions:
Write a remote method for each special query, which initializes the required fields (e.g. Hibernate.initialize()) and returns the domain objects to the client.
Like 1. but create DTOs
Split the query up into multiple queries, e.g. one for the customers, a second for the customers' articles, and let the client manage the results
The remote method takes a DetachedCriteria, which is created by the client and executed by the server
Develop a custom "Preload-Pattern", i.e. a way for the client to specify explicitly which properties to preload.
I have used 1 in the past and it worked well.
I think number 5 is why there is a "fetch" clause in HQL. Could you use that or is the problem more complex?
I've been at a customer who standardised its' projects on #5 and it worked really well. The final argument of a service call was a comma-separated list of all properties to be loaded, for example:
CustomerService.getCustomerById(id, "parent, address, address.city")
I believe they used the fetch clause for this. I implemented the same idea once for jpa using PropertyUtils to trigger the lazy loading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With