org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话 [英] org.hibernate.LazyInitializationException - could not initialize proxy - no Session

查看:104
本文介绍了org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下异常:

线程mainorg.hibernate中的异常。 LazyInitializationException:无法初始化代理 - 没有会话
在org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
在org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215 )
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at sei.persistence.wf.entities.Element _ $$ _ jvstc68_47.getNote(Element _ $$ _ jvstc68_47 .java)
at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
at JSON_to_XML.main(JSON_to_XML.java:84)

当我尝试从main调用以下行时:

 模型subProcessModel = getModelByModelGroup(1112); 
System.out.println(subProcessModel.getElement()。getNote());

我实现了 getModelByModelGroup(int modelgroupid)方法首先是这样的:
$ b

  public static Model getModelByModelGroup(int modelGroupId,boolean openTransaction){

Session session = SessionFactoryHelper.getSessionFactory()。getCurrentSession();
Transaction tx = null;

if(openTransaction)
tx = session.getTransaction();

String responseMessage =;

尝试{
if(openTransaction)
tx.begin();
Query query = session.createQuery(from Model where modelGroup.id =:modelGroupId);
query.setParameter(modelGroupId,modelGroupId);
@SuppressWarnings(unchecked)
List< Model> modelList =(List< Model>)query.list();
Model model = null;
// Cerco il primo esercizio中的模型:idwf_model_type = 3
(Model m:modelList)
if(m.getModelType()。getId()== 3){
model = m;
休息;
}

if(model == null){
Object [] arrModels = modelList.toArray();
if(arrModels.length == 0)
throw new Exception(Non esiste);

model =(Model)arrModels [0];
}

if(openTransaction)
tx.commit();
返回模型;
$ b $ catch(Exception ex){
if(openTransaction)
tx.rollback();
ex.printStackTrace();
if(responseMessage.compareTo()== 0)
responseMessage =Error+ ex.getMessage();
返回null;
}

并得到了异常。然后朋友建议我总是测试会话并获取当前会话以避免此错误。所以我这样做了:

  public static Model getModelByModelGroup(int modelGroupId){

Session session = null ;
布尔值openSession = session == null;
Transaction tx = null;
if(openSession){
session = SessionFactoryHelper.getSessionFactory()。getCurrentSession();
tx = session.getTransaction();
}
String responseMessage =;

尝试{
if(openSession)
tx.begin();
Query query = session.createQuery(from Model where modelGroup.id =:modelGroupId);
query.setParameter(modelGroupId,modelGroupId);
@SuppressWarnings(unchecked)
List< Model> modelList =(List< Model>)query.list();
Model model = null; (模型m:模型列表)
if(m.getModelType()。getId()== 3){
model = m;
休息;
}

if(model == null){
Object [] arrModels = modelList.toArray();
if(arrModels.length == 0)
throw new RuntimeException(Non esiste);

model =(Model)arrModels [0];

if(openSession)
tx.commit();
返回模型;
$ b $ catch(RuntimeException ex){
if(openSession)
tx.rollback();
ex.printStackTrace();
if(responseMessage.compareTo()== 0)
responseMessage =Error+ ex.getMessage();
返回null;
}

}

但仍然会出现同样的错误。
我一直在阅读这个错误,并找到了一些可能的解决方案。其中之一是将lazyLoad设置为false,但我不允许这样做,这就是为什么我被建议控制会话。

解决方案

这里有什么问题是,当你提交事务时,你的会话管理配置被设置为关闭会话。检查是否有类似的内容:

 < property name =current_session_context_class> thread< / property> 

在您的配置中。



为了克服这个问题,你可以改变会话工厂的配置或者打开另一个会话,而不是要求那些懒加载的对象。但是我在这里建议的是在getModelByModelGroup本身中初始化这个懒惰的集合,并调用:

  Hibernate.initialize(subProcessModel.getElement )); 

当您仍然处于活动状态时。



<最后一件事。友好的建议。在你的方法中你有类似的东西:

pre $ for(Model m:modelList)
if(m.getModelType( ).getId()== 3){
model = m;
休息;
}

请检查此代码是否仅过滤类型为id等于3的模型查询语句只是上面几行。

更多阅读材料:

session factory configuration

.org / question / 45772 / hibernate-problem-session-is-closed />关闭session的问题


I get the following exception:

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)
    at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
    at JSON_to_XML.main(JSON_to_XML.java:84)

when I try to call from main the following lines:

Model subProcessModel = getModelByModelGroup(1112);
System.out.println(subProcessModel.getElement().getNote());

I implemented the getModelByModelGroup(int modelgroupid) method firstly like this :

    public static Model getModelByModelGroup(int modelGroupId, boolean openTransaction) {

        Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();     
        Transaction tx = null;

        if (openTransaction)
            tx = session.getTransaction();

        String responseMessage = "";

        try {
            if (openTransaction)            
                tx.begin();
            Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
            query.setParameter("modelGroupId", modelGroupId);
            @SuppressWarnings("unchecked")
            List<Model> modelList = (List<Model>)query.list(); 
            Model model = null;
            // Cerco il primo Model che è in esercizio: idwf_model_type = 3
            for (Model m : modelList)
                if (m.getModelType().getId() == 3) {
                    model = m;
                    break;
                }

            if (model == null) {
                Object[] arrModels = modelList.toArray();
                if (arrModels.length == 0) 
                    throw new Exception("Non esiste ");

                model = (Model)arrModels[0];
            }

            if (openTransaction)
                tx.commit();
            return model;

        } catch(Exception ex) {
            if (openTransaction)
                tx.rollback();
            ex.printStackTrace();
            if (responseMessage.compareTo("") == 0)
                responseMessage = "Error" + ex.getMessage();
            return null;        
        }

and got the exception. Then a friend suggested me to always test the session and get the current session to avoid this error. So i did this:

public static Model getModelByModelGroup(int modelGroupId) {

        Session session = null;
        boolean openSession = session == null;
        Transaction tx = null;
        if (openSession){
          session = SessionFactoryHelper.getSessionFactory().getCurrentSession();   
            tx = session.getTransaction();
        }
        String responseMessage = "";

        try {
            if (openSession)            
                tx.begin();
            Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
            query.setParameter("modelGroupId", modelGroupId);
            @SuppressWarnings("unchecked")
            List<Model> modelList = (List<Model>)query.list(); 
            Model model = null;
            for (Model m : modelList)
                if (m.getModelType().getId() == 3) {
                    model = m;
                    break;
                }

            if (model == null) {
                Object[] arrModels = modelList.toArray();
                if (arrModels.length == 0) 
                    throw new RuntimeException("Non esiste");

                model = (Model)arrModels[0];

            if (openSession)
                tx.commit();
            return model;

        } catch(RuntimeException ex) {
            if (openSession)
                tx.rollback();
            ex.printStackTrace();
            if (responseMessage.compareTo("") == 0)
                responseMessage = "Error" + ex.getMessage();
            return null;        
        }

    }

but still get the same error. I have been reading a lot for this error and found some possible solutions. One of them was to set lazyLoad to false but I am not allowed to do this thats why i was suggested to control the session

解决方案

What is wrong here is that your session management configuration is set to close session when you commit transaction. Check if you have something like:

<property name="current_session_context_class">thread</property> 

in your configuration.

In order to overcome this problem you could change the configuration of session factory or open another session and only than ask for those lazy loaded objects. But what I would suggest here is to initialize this lazy collection in getModelByModelGroup itself and call:

Hibernate.initialize(subProcessModel.getElement());

when you are still in active session.

And one last thing. A friendly advice. You have something like this in your method:

            for (Model m : modelList)
            if (m.getModelType().getId() == 3) {
                model = m;
                break;
            }

Please insted of this code just filter those models with type id equal to 3 in the query statement just couple of lines above.

Some more reading:

session factory configuration

problem with closed session

这篇关于org.hibernate.LazyInitializationException - 无法初始化代理 - 没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆