我得到以下异常:

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)

当我试图从主要呼叫以下线路:

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

我首先实现了getModelByModelGroup(int modelgroupid)方法,如下所示:

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

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

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

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

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

但还是得到相同的错误。 我已经阅读了很多关于这个错误的文章,并找到了一些可能的解决方案。其中之一是将lazyLoad设置为false,但我不允许这样做,这就是为什么我被建议控制会话


当前回答

所有关于添加JOIN FETCH(或左JOIN FETCH)的答案都是正确的,我只想添加这个: 如果你有转换器,请确保getAsObject子使用一个“查找”,而不是包括在sql Join Fetch太。 我花了很多时间来修复一个类似的问题,问题是在转换器。

其他回答

你也可以通过在你的*.hbm.xml文件中添加lazy=false来解决这个问题,或者你可以在Hibernate.init(object)中初始化你的对象,当你从db中获取对象时

这意味着您在代码中使用JPA或hibernate,并在DB上执行修改操作,而不进行业务逻辑事务。 因此,简单的解决方案是将代码段标记为@Transactional

当我试图获取所有department时,JAX-RS应用程序中出现了这个错误。我必须将@JsonbTransient Annotation添加到这两个类的属性中。我的实体是Department和Employee,数据库关系是多对多。

Employee.java

...
@ManyToMany
@JoinTable(
        name = "emp_dept",
        joinColumns = {@JoinColumn(name = "emp_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "dept_id", referencedColumnName = "id")}
)
@JsonbTransient
private Set<Department> departments = new HashSet<Department>();
...

Department.java

...
@ManyToMany(mappedBy = "departments")
@JsonbTransient
private Set<Employee> employees = new HashSet<Employee>();
...

所有关于添加JOIN FETCH(或左JOIN FETCH)的答案都是正确的,我只想添加这个: 如果你有转换器,请确保getAsObject子使用一个“查找”,而不是包括在sql Join Fetch太。 我花了很多时间来修复一个类似的问题,问题是在转换器。

如果您使用spring data jpa, spring boot,您可以在application.properties中添加这一行

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true