未能懒惰地初始化角色集合:org.teleprovision.domain.CmdMaster.paramMastersList,没有会话或会话被关闭

时间:2014-08-24 05:21:45

标签: hibernate session

它会打印CmdMaster的大小和值,但我怎样才能获得paramMaster的值。它会给我错误

 "org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.teleprovision.domain.CmdMaster.paramMastersList, no session or session was closed"

插入和删除工作正常但是当selectSeviceActionById方法调用时会给出错误。

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

1 个答案:

答案 0 :(得分:0)

在您的代码中,paramMastersList默认为Lazy,尝试使用Eager或尝试使用Hibernate.intialize(),然后再使用paramMastersList

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "cmdMaster_cid")
private List<ParamMasters> paramMastersList ;