2

I have a server and clients that are both Java-based and communicate with each other over RMI. The server's data access layer is implemented with JPA and Hibernate and entities are often used as parameters.

The clients do not have Hibernate code and dynamic code download is disabled, so when the server sends an entity to its clients, the actual entity must be sent, not the proxy. Moreover, the entity must be sent as-is, without pre-loading all lazy-fetched attributes because they will never be needed by the clients in most cases.

Is it possible to implement these requirements with Hibernate?

Tom Tucker
  • 11,676
  • 22
  • 89
  • 130
  • maybe this helps: http://stackoverflow.com/questions/11228838/how-to-unproxy-a-hibernate-object – Korgen Jul 15 '13 at 16:06
  • @Korgen Thanks, but doesn't getHibernateLazyInitializer() pre-fetch all referenced entities? – Tom Tucker Jul 15 '13 at 16:29
  • from what I see from the API you'd have to call the initialize method to fetch them (http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/proxy/LazyInitializer.html) – Korgen Jul 16 '13 at 06:58
  • 1
    My [answer to a similar question](http://stackoverflow.com/a/14135708/687514) might work for you. – Anders R. Bystrup Jul 22 '13 at 13:54

2 Answers2

0

you can fetch all properties in the query

from docs

If you are using property-level lazy fetching (with bytecode instrumentation), it is possible to force Hibernate to fetch the lazy properties in the first query immediately using fetch all properties.

from Document fetch all properties order by name

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

or use eagle fetch in your mapping...

osdamv
  • 3,493
  • 2
  • 21
  • 33
0

The only clean way I know of (that is without fetching every association for the object) is to use a separate object layer for data transportation.

Alternatively you can send the same object types repopulated by you with the necessary data, instead of hibernates objects with the proxies set.

Thihara
  • 7,031
  • 2
  • 29
  • 56