A recommendation we found in Hibenate forum is that in most cases returning all the properties does not decrease performance. In our case we work with Swing applications and detached objects which we serialize to client apps. These objects must be lightweight and we avoid serializing properties we won't use.
Many messages in Hibernate forum recommends projection and lightweight pattern as solutions to the lightweight object issue.
We think another alternative is to use
"select new" feature.
We prefer to instatiate an object according to our needs instead of doing a projection for further filling an object. This way we can get lightweight objects from the query, without having to create additional hibernate mappings or java classes (lightweight pattern). So if we have 40 properties in Store class and just want 2 of them in some situation (remember we will serialize the object and transmit it over the network), we just do:
Code:
select new Store(store.id, store.description) from Store store
That's nice. Hibernate allows it. This feature is really wonderful!!!
Now we submit some sugestions to your appreciation:
1. If we want a lightweight Section object inside Store, we would have to do a
nested dynamic instatiation like this:
Code:
select new Store(store.id, store.description, new Section(store.section.id, store.section.name)) from Store store
That code is not allowed in Hibernate, since it does not support nested dynamic instantiation.
So our first sugestion is to include this feature in future Hibernate releases.
2. Another code that does not work in Hibernate is:
Code:
select new Store(store.id, store.description, elements(store.subsections)) from Store store
Hibernate "select new" does not allow
use of collections. It searches for the constructor Store(int id, String description, Subsection subsection) and not Store(int id, String description, Collection subsections).
That feature would be nice, since the properties of type Collection inside the new Store object are null and they cannot benefit from Hibernate Lazy Collection feature.
Hibernate is a fantastic persistence framework which is becoming even better.
We would like to hear from Hibernate team and from community about our sugestions. What is your opinion about them?