6

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?

starblue
  • 51,675
  • 14
  • 88
  • 146
leonm
  • 6,319
  • 27
  • 35

2 Answers2

6

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

osdamv
  • 3,355
  • 2
  • 18
  • 32
  • from the API: "A different way of avoiding unnecessary column reads, at least for read-only transactions, is to use the projection features of HQL or Criteria queries. This avoids the need for buildtime bytecode processing and is certainly a preferred solution." – ManuPK Nov 23 '11 at 02:46
  • i think you had not to work with legacy databases where you have tables of 50 columns i.i – osdamv Nov 23 '11 at 04:38
3

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.

Community
  • 1
  • 1
ManuPK
  • 10,995
  • 9
  • 54
  • 75