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