它会打印CmdMaster的大小和值,但我怎样才能获得paramMaster的值。它会给我错误
"org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.teleprovision.domain.CmdMaster.paramMastersList, no session or session was closed"
ServiceMst.java
@Entity(name = "ForeignKeyAssoEntity2")
@Table(name = "Service_Mst")
public class ServiceMst implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "aid", unique = true, nullable = false)
private Integer aid;
private String actionName;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL )
@JoinColumn(name="mst_aid")
private List<CmdMaster> cmdMasters;
//getters and setters
}
CmdMaster.java
@Entity(name = "ForeignKeyAssoAccountEntity1")
@Table(name = "Cmd_Master")
public class CmdMaster implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cid", unique = true, nullable = false)
private Integer cid;
private String actualCommand;;
@ManyToOne
private ServiceMst mst;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "cmdMaster_cid")
private List<ParamMasters> paramMastersList ;
}
ParamMasters.java
@Entity(name = "ForeignKeyAssoAccountEntity3")
@Table(name = "Param_Master")
public class ParamMasters {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pid", unique = true, nullable = false)
private Integer pid;
private String paramName;
private Integer requried;
@ManyToOne
private CmdMaster cmdMaster;
}
ServiceMstDao.java
public class ServiceMstDao {
public ServiceMst selectSeviceActionById(Integer id)
{
ServiceMst serviceMst2 = null;
try{
Session session= HibernateUtility.getSession();
serviceMst2=(ServiceMst) session.get(ServiceMst.class,id);
session.close();
}catch(Exception e)
{
e.printStackTrace();
}
return serviceMst2;
}
}
HibernateUtility.java
public class HibernateUtility {
public static SessionFactory factory;
static{
try{
Configuration cfg=new AnnotationConfiguration();
cfg.configure();
factory=cfg.buildSessionFactory();
}
catch(HibernateException he){
System.out.println(he);
}
}
public static SessionFactory getFactory() {
return factory;
}
public static Session getSession() {
Session session=null;
if(factory!=null){
session=factory.openSession();
}
return session;
}
}
这是我的 testUpdate.java
public static void main(String args[])
{
try
{
ServiceMstDao dao=new ServiceMstDao();
ServiceMst getSeviceMst=null;
getSeviceMst=dao.selectSeviceActionById(1);
System.out.println("servMst"+getSeviceMst.getActionName());
List<CmdMaster> cmdMstList=getSeviceMst.getCmdMasters();
System.out.println("cmd size=="+cmdMstList.size());
List<ParamMasters> params=new ArrayList<ParamMasters>();
List <ParamMasters>getpara=new ArrayList<ParamMasters>();
for(CmdMaster cm:cmdMstList)
{
params=cm.getParamMastersList();
getpara.addAll(params);
System.out.println("command=="+cm.getActualCommand());
}
for(ParamMasters pm:paramsgetpara)
{
System.out.println("param name=="+pm.getParamName());
}
}catch(Exception e)
{
e.printStackTrace();
}
}
答案 0 :(得分:0)
在您的代码中,paramMastersList
默认为Lazy
,尝试使用Eager
或尝试使用Hibernate.intialize()
,然后再使用paramMastersList
。
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "cmdMaster_cid")
private List<ParamMasters> paramMastersList ;