コード
l_detailsのピンには、 x_details 主キーを参照する外部制約があります。ここでは、XDetails クラスからの一方向の関係を使用します。
エンティティ:
@Entity
@Table(name = "x_details")
public class XDetails implements Lifecycle {
private int y;
....
.....
private Set<LDetails> lDetails = new HashSet<LDetails>(0);
.........
..........
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="pin")
public Set<LDetails> getLDetails() {
return this.lDetails;
}
@Override
public void onLoad(Session s, Serializable id) {
System.out.println("inside load");
try{
if((lDetails!=null)){
System.out.println("size of lDetails"+lDetails.size());
}
}
catch(Exception exception){
exception.printStackTrace();
}
}
}
@Entity
@Table(name = "l_details")
public class LDetails {
private Integer id1;
private Integer pin;
....
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id1", unique = true, nullable = false)
public Integer getId1() {
return this.id1;
}
public void setId1(Integer id1) {
this.id1 = id1;
}
@Column(name="pin")
public Integer getPin() {
return this.pin;
}
public void setPin(Integer pin) {
this.pin = pin;
}
}
メインスレッド:
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Criteria criteria=session.createCriteria(XDetails.class);
criteria.createAlias("lDetails", "status",CriteriaSpecification.INNER_JOIN);
criteria.setFetchMode("status", FetchMode.JOIN);
List<XDetails> details=criteria.list();
session.getTransaction().commit();
例外 ( onload 内):
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at rg.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:162)
at com.hibernate.test.pendingdict3.XDetails.onLoad(XDetails.java:588)
at org.hibernate.event.def.DefaultPostLoadEventListener.onPostLoad(DefaultPostLoadEventListener.java:73)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:234)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at com.hibernate.test.main4.HibernateStandaloneMain.main(HibernateStandaloneMain.java:24)
Hibernate 3.5.6 の最終的な依存関係である mysql データベースを使用しており、hibernatestandalone アプリケーションを実行しています。
# onload method
@Override
public void onLoad(Session s, Serializable id) {
System.out.println("inside load");
try{
if((lDetails!=null)){
System.out.println("size of lDetails"+lDetails.size());
}
}
catch(Exception exception){
exception.printStackTrace();
}
}