How to initialize Hibernate objects received by a remote method call?

When you call a remote service (for example, over RMI) to load a list of entities from a database using Hibernate, how do you manage it to initialize all the fields and links the client needs?

Example. The client calls the remote method to load all clients. With each client, the client wants the link to the list of purchased goods of the client to be initialized.

I can imagine the following solutions:

  • Write a remote method for each special request that initializes the required fields (for example, Hibernate.initialize ()) and returns domain objects to the client.

  • Like 1. but create a DTO

  • Divide the query into multiple queries, for example. one for customers, the second for customer articles and allows the customer to manage results.

  • The remote method uses DetachedCriteria, which is created by the client and executed by the server

  • Develop a custom "Preload-Pattern", that is, a way for the client to explicitly specify which properties should be preloaded.

+6
java hibernate remoting rmi
source share
5 answers

I used 1 in the past and it worked well.

+1
source share

I think the number 5 is why there is a fetch clause in HQL. Could you use this or the problem is more complicated?

+1
source share

I was with a client who standardized his "projects at No. 5," and he worked very well. The last argument to the service call was a list of all loaded properties, separated by commas, for example:

CustomerService.getCustomerById(id, "parent, address, address.city") 

I believe the fetch clause was used for this. I implemented the same idea as for jpa, using PropertyUtils to start lazy loading.

+1
source share

This remote service ... is this another part of the same application? if so, there is no problem sharing classes and sending instances back and forth (same for RMI using stubs).

Then there is the possibility of access to a web service or something like that, when two applications that exchange messages do not have classes, and this makes things even more complicated. I am going to face this problem in the near future, and I will be very interested to know the answer.

Anyway, my vote is for the DTO. I think they provide the best and easiest solution where applicable, and help keep the code simple and convenient.

Yuval = 8 -)

0
source share

If your remote service exists only to provide data to the client, then you might need to disable lazy loading for all Hibernate objects.

Personally, however, I believe that the DTO is the right way. Expressing your remote interface in terms of DTO, you are sure that you get everything you need and did not expect anything.

0
source share

All Articles