FetchType.LAZY doesn't work
psmith Apr 25, 2006 12:48 AMI have two beans A and B
package au.com.pulse.online.persistence.ejb.crm;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@javax.persistence.Entity
@Table(name = "A")
public class A {
private long col1;
private String col2;
private String col3;
private List bvals;
@Id()
@GeneratedValue( strategy=GenerationType.AUTO )
@Column(name = "COL_1")
public long getCol1() {
return col1;
}
public void setCol1(long col1) {
this.col1 = col1;
}
@Column(name = "COL_2")
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
@Column(name = "COL_3")
public String getCol3() {
return col3;
}
public void setCol3(String col3) {
this.col3 = col3;
}
@OneToMany(fetch = FetchType.LAZY)
@Column(name="COL_1")
@JoinColumn(name="COL_2")
public List getBs() {
return bvals;
}
public void setBs(List bvals) {
this.bvals = bvals;
}
}
package au.com.pulse.online.persistence.ejb.crm;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@javax.persistence.Entity
@Table(name = "B")
public class B {
private long col1;
private long col2;
private String col3;
@Id()
@GeneratedValue( strategy=GenerationType.AUTO )
@Column(name = "COL_1")
public long getCol1() {
return col1;
}
public void setCol1(long col1) {
this.col1 = col1;
}
@Column(name = "COL_2")
public long getCol2() {
return col2;
}
public void setCol2(long col2) {
this.col2 = col2;
}
@Column(name = "COL_3")
public String getCol3() {
return col3;
}
public void setCol3(String col3) {
this.col3 = col3;
}
}
And a session bean that has the following method:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public A getA() {
try {
A a = (A) em.createQuery("from A where col1 = 1").getSingleResult();
List bs = a.getBs();
return a;
} catch (Exception e) {
log.info(e);
return null;
}
}
If I define the following:
@OneToMany(fetch = FetchType.EAGER)
then the list of B's gets populated when the query runs. If I leave it as LAZY then it never gets populated. Does anybody know what I'm missing here?