-->
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.  [ 2 posts ] 
Author Message
 Post subject: Lazy fetch on one-to-many collections not working.
PostPosted: Thu Jan 24, 2008 8:48 pm 
Newbie

Joined: Sun Jan 02, 2005 10:17 pm
Posts: 13
Hibernate version: 3.2.5

Hibernate Annotations version: 3.3.0.GA

Name and version of the database you are using: Oracle 10g (Oracle JDBC driver version 10.1.0.2.0)


Hello,

I have an entity class, "Person", which have multiple one-to-many relationships with other entities ("CreditCardDebt" and "MortageDebt"). Both classes "CreditCardDebt" and "MortageDebt" extends from an abstract class named "Debts". The persistence mapping with classes "Debt", "CreditCardDebt", and "MortageDebt" is SINGLE_TABLE. In the "Person" class, I've mapped the one-to-many relationships using lazy fetch mode.

My problem is, when I try to retrieve a Person entity by primary key, these one-to-many collections are retrieved as well (i.e. NOT lazy loaded) even though I've specified "fetch=FetchType.LAZY" in the mapping. Lazy fetching on these one-to-many collections doesn't seem to be working for me.

My code is as follows:

Debt class:

Code:
@Entity
@Table(name="debt")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="debt_type", discriminatorType=DiscriminatorType.STRING)
@ForceDiscriminator
public abstract class Debt {
   protected int id;
   protected float debtAmount;
   protected Person person;

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
   public int getId() {
      return id;
   }

   private void setId(int id) {
      this.id = id;
   }

   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="person_id")
   public Person getPerson() {
      return this.person;
   }

   public void setPerson(Person p) {
     this.person = p;
   }

   public float getDebtAmount() {
     return this.debtAmount;
   }

   public void setDebtAmount(float amount) {
     this.debtAmount = amount;
   }

}


CreditCardDebt class:

Code:
@Entity
@DiscriminatorValue("CC")
public class CreditCardDebt extends Debt {
   
   // some irrelevant code here
}



MortageDebt class:

Code:
@Entity
@DiscriminatorValue("MD")
public class MortageDebt extends Debt {

  // some more irrelevant code here
}



Person class;

Code:
@Entity
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {

   protected int id;
   protected List<CreditCardDebt> ccDebts;
   protected List<MortageDebt> mortageDebts;

   public Person() {
      ccDebts = new ArrayList<CreditCardDebt>();
      mortageDebts = new Arraylist<MortageDebt>();
   }

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
   public int getId() {
      return id;
   }

   private void setId(int id) {
      this.id = id;
   }

   @OneToMany(targetEntity=Debt.class, mappedBy="person",
      cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   public List<CreditCardDebt> getCcDebts() {
      return this.ccDebts;
   }

   public void setCcDebts(List<CreditCardDebt> debts) {
      this.ccDebts = debts;
   }

   @OneToMany(targetEntity=Debt.class, mappedBy="person",
      cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   public List<MortageDebt> getMortageDebts() {
      return this.mortageDebts;
   }

   public void setMortageDebts(List<MortageDebt> debts) {
      this.mortageDebts = debts;
   }
}


I have even tried using the annotation @LazyCollection(LazyCollectionOption.EXTRA) on the one-to-many collection mappings. However, result was still the same: when I get a Person entity from the database, the collections of debts are fetched along with the Person entity.

I'm using the session.get(Class clazz, Serializable id) method to get Person entities by primary key. I know this session.get() method will actually initialize the Person entity. However, my understanding is that session.get() will not initialize the collections associated with Person entity if they are mapped with fetch=FetchType.LAZY. Is this not true? Is there something wrong with my mappings?

Thanks for the help,
IK


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 10:09 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
How do you know the collections are not fetched lazily?

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.