Hibernate org.hibernate.LazyInitializationException:未能延迟初始化角色集合: [英] Hibernate org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:

查看:138
本文介绍了Hibernate org.hibernate.LazyInitializationException:未能延迟初始化角色集合:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面提到的实体类,当我执行我的应用程序时,我收到以下异常。一些其他类似的问题没有解决问题。

 警告:StandardWrapperValve [jersey-serlvet]:PWC1406:Servlet。 service()
对于servlet jersey-serlvet抛出异常
org.hibernate.LazyInitializationException:未能懒惰地初始化
a集合中的角色:test.entity.Dept.empDeptno,没有会话
或者会话在org.hibernate.collection.internal.AbstractPersistentCollection处关闭了

throwLazyInitializationException(AbstractPersistentCollection.java:393)$ or $ $ b $ org.hibernate.collection.internal.AbstractPersistentCollection。
throwLazyInitializationExceptionIfNotConnected
(AbstractPersistentCollection.java:385)
位于org.hibernate.collection.internal.AbstractPersistentCollection。
initialize(AbstractPersistentCollection.java:378)

我该如何解决这个问题? p>

雇主实体

  @Entity 
@Table(name =EMP,schema =SCOTT

@XmlRootElement
@NamedQueries({
@NamedQuery(name =Emp.findAllEmployees, query =select e from emp e left
join fetch e.deptNo order by e.empno desc)
})
public class Emp implements java.io.Serializable {
@I $
@Column(name =EMPNO,unique = true,nullable = false,precision = 4,
scale = 0)
private short empno;
@ManyToOne
@JoinColumn(name =DEPTNO,referencedColumnName =DEPTNO)
private Dept deptNo;

部门实体

  @Entity 
@Table(name =DEPT,schema =SCOTT

@XmlRootElement
public class Dept实现java.io.Serializable {
@Id
@Column(name =DEPTNO,unique = true,nullable = false,precision = 2,
scale = 0)
私人短的deptno;
@OneToMany(fetch = FetchType.LAZY,mappedBy =deptNo)
private Set< Emp> empDeptno;

DAOImpl

  @Override 
public List< Emp> findAllEmployees(){
return getEntityManager()。createNamedQuery(Emp.findAllEmployees,
Emp.class).getResultList();
}

泽西RESTful服务

  @Component 
@Path(/ employee)
public class EmployeeRestService {

@Autowired
EmployeeService employeeService;

@GET
@Produces({MediaType.APPLICATION_JSON})
public List< Emp> getEmployees(){
List< Emp> emp = new ArrayList< Emp>();
emp.addAll(getEmployeeService()。findAllEmployees());
return emp;

Spring applicationContext.xml

 < beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http: //www.w3.org/2001/XMLSchema-instancexmlns:tx =http://www.springframework.org/schema/tx
xmlns:context =http://www.springframework。 org / schema / context
xsi:schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans- 3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http ://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
>
<! - 数据源声明 - >
< bean id =DataSourceclass =org.springframework.jndi.JndiObjectFactoryBean>
< property name =jndiNamevalue =jdbc / scottDS/>
< / bean>

< context:component-scan base-package =net.test/>
< bean class =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor/>
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =DataSource/>
< property name =packagesToScanvalue =net.test/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =showSqlvalue =false/>
< property name =generateDdlvalue =false/>
< property name =databasePlatformvalue =$ {jdbc.dialectClass}/>
< / bean>
< / property>
< / bean>
< bean id =defaultLobHandlerclass =org.springframework.jdbc.support.lob.DefaultLobHandler/>
<! - 交易配置 - >
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
< context:annotation-config />
< bean id =hibernateStatisticsMBeanclass =org.hibernate.jmx.StatisticsService>
< property name =statisticsEnabledvalue =true/>
< property name =sessionFactoryvalue =#{entityManagerFactory.sessionFactory}/>
< / bean>
< / beans>


解决方案

.xml

 < filter> 
< filter-name> OpenEntityManagerInViewFilter< / filter-name>
< filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> OpenEntityManagerInViewFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

礼貌这里这里



谢谢


I have the below mentioned Entity classes, when I execute my application I am getting the following exception. Some of the other similar questions didn't solve the problem.

WARNING: StandardWrapperValve[jersey-serlvet]: PWC1406: Servlet.service()
for servlet jersey-serlvet threw exception
org.hibernate.LazyInitializationException: failed to lazily initialize 
a collection of role: test.entity.Dept.empDeptno, no session
or session was closed
at org.hibernate.collection.internal.AbstractPersistentCollection.
throwLazyInitializationException(AbstractPersistentCollection.java:393)
       at    org.hibernate.collection.internal.AbstractPersistentCollection.
throwLazyInitializationExceptionIfNotConnected
(AbstractPersistentCollection.java:385)
    at org.hibernate.collection.internal.AbstractPersistentCollection.
initialize(AbstractPersistentCollection.java:378) 

How can I solve this issue?

Emp Entity

@Entity
@Table(name = "EMP", schema = "SCOTT"
)
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Emp.findAllEmployees", query = "select e from Emp e left 
    join fetch e.deptNo order by e.empno desc")
})
public class Emp implements java.io.Serializable {
@Id
@Column(name = "EMPNO", unique = true, nullable = false, precision = 4,
scale = 0)
private short empno;
@ManyToOne
@JoinColumn(name = "DEPTNO", referencedColumnName = "DEPTNO")
private Dept deptNo;

Dept Entity

@Entity
@Table(name = "DEPT", schema = "SCOTT"
)
@XmlRootElement
public class Dept implements java.io.Serializable {
@Id
@Column(name = "DEPTNO", unique = true, nullable = false, precision = 2,
scale = 0)
private short deptno;
@OneToMany(fetch=FetchType.LAZY,mappedBy = "deptNo")
private Set<Emp> empDeptno;

DAOImpl

@Override
public List<Emp> findAllEmployees() {
  return getEntityManager().createNamedQuery("Emp.findAllEmployees",
 Emp.class).getResultList();
}

Jersey RESTful service

 @Component
 @Path("/employee")
 public class EmployeeRestService {

 @Autowired
 EmployeeService employeeService;

 @GET
 @Produces({MediaType.APPLICATION_JSON})
 public List<Emp> getEmployees() {
 List<Emp> emp = new ArrayList<Emp>();
 emp.addAll(getEmployeeService().findAllEmployees());
 return emp;
 }

Spring applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
    <!-- Data Source Declaration -->    
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/scottDS"/>   
    </bean>

    <context:component-scan base-package="net.test" />
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="packagesToScan" value="net.test" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="${jdbc.dialectClass}" />
            </bean>
        </property>
    </bean>
    <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />  
    <!-- Transaction Config -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>          
    <context:annotation-config/>
    <bean id="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled" value="true" />
        <property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" />
    </bean>
</beans>

解决方案

I have resolved the issue by adding the following in web.xml

<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

courtesy here and here

Thanks

这篇关于Hibernate org.hibernate.LazyInitializationException:未能延迟初始化角色集合:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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