当我运行以下代码时,我不断收到此错误“线程“主”org.hibernate.LazyInitializationException 中的异常:无法初始化代理 - 拥有的会话已关闭”:
public ArrayList<ProfileDTO> getInitialProfiles(Contracts ct){
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
ArrayList<ProfileDTO> profileDTOs=new ArrayList<ProfileDTO>();
try{
Hibernate.initialize(ct);
SQLQuery query=session.createSQLQuery("select {b.*},{p.*},{t.*} from bidtool.bt_boiler_plates b,bidtool.bt_profile p,bidtool.bt_trade_lane t where b.contract_id=:val AND p.contract_id=:val AND t.contract_id=:val")
.addEntity("b",Boiler_Plates.class)
.addEntity("p",BidToolProfiles.class)
.addEntity("t",BidToolTradeLanes.class);
query.setParameter("val", ct.getContract_id());
List list=query.list();
Iterator iteContract = list.iterator();
while ( iteContract.hasNext() ) {
Object[] pair =(Object[]) iteContract.next();
Boiler_Plates bp=(Boiler_Plates)pair[0];
BidToolProfiles p=(BidToolProfiles)pair[1];
ProfileDTO profileDTO=new ProfileDTO();
profileDTO.setProfileId(p.getProfileId());
profileDTO.setBt_contracts(p.getBt_contracts());
profileDTO.setCreated(p.getCreated());
profileDTO.setProfileContent(p.getProfileContent());
profileDTO.setEditable(p.getEditable());
profileDTO.setProfileName(p.getProfileName());
profileDTOs.add(profileDTO);
}
return profileDTOs;
}
catch(Exception ex){
System.out.println(ex.getMessage());
return profileDTOs;
}
finally{
session.flush();
session.close();
}
}
只要我不关闭会话,它就可以正常工作,但我不能这样做。我需要关闭会话。您的帮助将不胜感激。谢谢你。