代码
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();
}
}