-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Getting a smaller subset of compositeEntity
PostPosted: Thu Apr 01, 2010 1:27 pm 
Newbie

Joined: Thu Apr 01, 2010 1:05 pm
Posts: 4
Hello,

Rearching on achieving following using hibernate. Did not get much help online. Any suggesations will be appreciated.

We have CompositeEnty which holds several sub entities. Right now we have several views for this compositeEntity. A view is a smaller subset of CompositeEntity entities retreived from the database.

EX: Employee(Compoiste Entity)

Subentities of Employee are: Address, SalaryInfo,WorkInfo, EducationInfo

Sometime we don't want to fetch complete employee. Instead we want to fetch Employee with addess and SalaryInfo or Employee with WorkInfo, EducationInfo and SalaryInfo.

Right now we have achieved this using java and DAO. Replacing DAO with hibernate in Project. Not sure how to implement above scenario using hibernate?


Top
 Profile  
 
 Post subject: Re: Getting a smaller subset of compositeEntity
PostPosted: Fri Apr 02, 2010 4:02 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Sometime we don't want to fetch complete employee. Instead we want to fetch Employee with addess and SalaryInfo or Employee with WorkInfo, EducationInfo and SalaryInfo.


in this case I recommend you to define all relations Employee --> Addess , Employee-->WorkInfo etc. as LAZY.
In this way the regarding data is fetched only when you effectively access to it.


Top
 Profile  
 
 Post subject: Re: Getting a smaller subset of compositeEntity
PostPosted: Fri Apr 02, 2010 9:33 am 
Newbie

Joined: Thu Apr 01, 2010 1:05 pm
Posts: 4
Thanks much expert.Can you explain with an example? I can understand better.


Top
 Profile  
 
 Post subject: Re: Getting a smaller subset of compositeEntity
PostPosted: Fri Apr 02, 2010 10:12 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
With eager you have following behavior:

Code:
@Entity A {
...
@javax.persistence.ManyToOne(fetch= FetchType.EAGER)
public D assSingleD = null;
}


Code:
a = (A) s.get(A.class, id_a); // a and d are read together with a join query, see below


Code:
    select
        a0_.oid as oid2_1_,
        a0_.assSingleD_oid as assSingleD4_2_1_,
        a0_.name as name2_1_,
        a0_.version as version2_1_,
        d1_.oid as oid5_0_,
        d1_.assA_oid as assA3_5_0_,
        d1_.version as version5_0_
    from
        A a0_
    left outer join
        D d1_
            on a0_.assSingleD_oid=d1_.oid
    where
        a0_.oid=1


Code:
      D d = a.getAssSingleD(); // not database hit, d is already loaded
          
      System.out.println(d.getVersion());





With lazy indeed you have following behavior:

Code:
@Entity A {
...
@javax.persistence.ManyToOne(fetch= FetchType.LAZY)
public D assSingleD = null;
}


Code:
a = (A) s.get(A.class, id_a); // only a is selected from database, see below


Code:
select
        a0_.oid as oid2_0_,
        a0_.assSingleD_oid as assSingleD4_2_0_,
        a0_.name as name2_0_,
        a0_.version as version2_0_
    from
        A a0_
    where
        a0_.oid=1


Code:
      D d = a.getAssSingleD(); // not database hit, d is a hibernate proxy
          
      System.out.println(d.getVersion());  // only here where I access informations of d, the query on d is executed, see below


Code:
    select
        d0_.oid as oid5_0_,
        d0_.assA_oid as assA3_5_0_,
        d0_.version as version5_0_
    from
        D d0_
    where
        d0_.oid=1


I Hope this is clear someway.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.