Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Hibernate Embedded Object be lazy loaded?

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?

like image 708
leonm Avatar asked Nov 22 '11 00:11

leonm


2 Answers

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

like image 184
osdamv Avatar answered Sep 28 '22 09:09

osdamv


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:

  1. 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.

  2. 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.

like image 45
ManuPK Avatar answered Sep 28 '22 11:09

ManuPK