org.springframework.orm.hibernate4.HibernateSystemException:非法尝试将代理与两个打开的Sessions关联

时间:2014-11-10 13:30:41

标签: java spring hibernate session spring-mvc

我收到org.springframework.orm.hibernate4.HibernateSystemException:非法尝试在更新实体时将代理与两个打开的Sessions Exception关联。我尝试了很多方法来配置我的事务管理器,但仍然是相同的。没有得到我错误的地方...... :(

Category.java

    @Entity
@Table(name = "oc_category")
public class Category implements java.io.Serializable {
    private Long categoryId;
    private String image = "web-resources/image/no_image-45x45.png";
    private Integer parentId = 0;
    private Integer column = 1;
    private Integer sortOrder = 0;
    private Boolean status = false;
    private Date dateAdded;
    private Date dateModified;
    private CategoryDescription categoryDescription;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "category_id", unique = true, nullable = false)
    public Long getCategoryId() {
        return categoryId;
    }
    .
    .
    .
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "category_desc_id")
    public CategoryDescription getCategoryDescription() {
        return categoryDescription;
    }
    .

CategoryDe​​scription.java

        @Entity
    @Table(name = "oc_category_description")
    public class CategoryDescription implements java.io.Serializable {
        private Long categoryDescId;
        private String name;
        private String description;
        private String metaTitle;
        private String metaDescription;
        private String metaKeyword;
        private Language language;
        private Store store;


        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "category_desc_id", unique = true, nullable = false)
        public Long getCategoryDescId() {
            return categoryDescId;
        }

来自Controller我从save方法调用 categoryService.save(category);

我的服务impl类是

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    @Resource(name="categoryDAO")
    private CategoryDAO categoryDAO;
        public void setCategoryDAO(CategoryDAO categoryDAO) {
    this.categoryDAO = categoryDAO;
    }
   @Transactional
   public void save(Category entity) {
   categoryDAO.save(entity);
   }
}

Dao impl是

@Repository(value = "categoryDAO")
public class CategoryDAOImpl implements CategoryDAO{
@Autowired
SessionFactory sessionFactory;

@Override
public void save(Category entity) {
    try {
        sessionFactory.getCurrentSession().saveOrUpdate(entity);
    } catch (Exception exception)
    {
        exception.printStackTrace();
    }
}
}

最后配置文件是

    <tx:annotation-driven />

<context:component-scan base-package="com" />

<context:property-placeholder location="classpath:database.properties" />
<mvc:annotation-driven />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">  
        <list>  
            <value>com.beingjavaguys.domain.Employee</value>  
            <value>com.cart.model.Category</value>
            <value>com.cart.model.CategoryDescription</value>  
            <value>com.cart.model.Language</value>  
            <value>com.cart.model.Store</value>  
        </list>  
    </property> 
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory">
</bean>

保存新类别,CategoryDe​​scription工作正常,同时点击编辑并再次保存,获取 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.orm.hibernate4.HibernateSystemException:非法尝试将代理与两个打开的Sessions关联;嵌套异常是org.hibernate.HibernateException:非法尝试将代理与两个打开的会话相关联

从哪里开始2场会议,请帮忙

完整的堆栈跟踪

11:22:47,394 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/myproject2].[mvc-dispatcher]] (http-/0.0.0.0:8080-2) JBWEB000236: Servlet.service() for servlet mvc-dispatcher threw exception: org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
    at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:124) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.StatefulPersistenceContext.reassociateProxy(StatefulPersistenceContext.java:566) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.StatefulPersistenceContext.reassociateIfUninitializedProxy(StatefulPersistenceContext.java:531) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.ProxyVisitor.processEntity(ProxyVisitor.java:49) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:124) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:82) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractVisitor.process(AbstractVisitor.java:143) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:310) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:244) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:167) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:158) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:555) [spring-orm-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at com.sun.proxy.$Proxy43.save(Unknown Source)
    at com.cart.admin.controller.CategoryController.saveCategory(CategoryController.java:110) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_67]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
    at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_67]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) [spring-webmvc-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.2.1.Final-redhat-10.jar:7.2.1.Final-redhat-10]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_67]

0 个答案:

没有答案