I have a Hibernate Entity that have a couple of embedded objects that are quite fat but infrequently used. I'd like to make the embedded objects Lazy Loaded but I would ideally not want to move the information to separate tables.
Is it possible, and how do I annotate the embedded object to be lazily loaded?
directly you cant, by setting attributes in the object yes you can
@Basic(fetch=FetchType.LAZY)
and also you should read http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties
As I understand from your question you have a large object(or table) which you don't have want to fill all the properties. You can use the projection features of HQL or Criteria queries as told here.
Here is an example, the HQL should be,
select new com.foo.Bean(b.prop1,b.prop2,...) from Bean b
Also you need to add a corresponding constructor in the Bean
class.
Let me add few more things:
lazy
in hibernate has meaning when it has to fetch the data from multiple tables. Here you save the additional query to be fired to fetch the data from additional tables. Read more about the lazy
settings here. It may not be applicable in your case as you have to fetch the data only from one table.
I don't feel it is a good idea to have multiple objects to represent the data in the same table, only because the data in few columns are rarely used.
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