gpt4 book ai didi

java - TransactionrequireException,没有正在进行的交易

转载 作者:行者123 更新时间:2023-12-01 13:10:56 25 4
gpt4 key购买 nike

我目前正在开发 Maven + Spring + Hibernate 项目。实际上,这只是一个测试项目,只是为了熟悉 Spring 如何与 Hibernate (+Maven) 配合使用。我已经设置并准备了必要的依赖项。即 Spring 的 appcontext.xml、Hibernate 的 persistence.xml、JPA/Persistence/Hibernate 的实体和 DAO 对象。

在调试期间,persist 方法抛出 TransactionrequireException,没有事务正在进行。我不知道是什么原因造成的

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-datasource.xml
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

application-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />



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


<!-- Default locale set -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>

<!-- Tell Spring to not try to map things in these directories to controllers -->
<!-- Order must be set to supercede the handler configured by the mvc:annotation-driven annotation -->
<mvc:resources order="-10" location="/img/" mapping="/img/**" />
<mvc:resources order="-10" location="/css/" mapping="/css/**" />
<mvc:resources order="-10" location="/js/" mapping="/js/**" />
<mvc:resources order="-10" location="/fonts/" mapping="/fonts/**" />
<mvc:resources order="-10" location="favicon.ico" mapping="favicon.ico" />
<mvc:resources order="-10" location="robots.txt" mapping="robots.txt" />

</beans>

applicationContext-datasource.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- The "webDS" data source is the main data source for names. It is referenced and
should be configured via JNDI in your particular environment. -->

<jee:jndi-lookup id="datasource" jndi-name="jdbc/web"/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<tx:annotation-driven transaction-manager="transactionManager" />

<context:annotation-config/>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory" >
<property name="persistenceUnitName" value="wzpu"/>
<property name="dataSource" ref="datasource" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

admincontroller.java

package com.names.home;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class AdminController {

@Resource(name="profiledao")
protected ProfileDao profiledao;

@RequestMapping(value="/admin/insert")
@Transactional
public String insert(){
Profile pr = new Profile();
pr.setId(1);
profiledao.save(pr);
return "home";
}
}

ProfileDao.java

package com.names.home;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@Repository
public class ProfileDao {

@PersistenceContext(unitName = "wzpu")
protected EntityManager em;

public Profile find(){
return this.em.find(Profile.class, 2);
}

public void save(Profile profile){
this.em.persist(profile);
em.flush();
}
}

配置文件.java

package com.names.home;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "profile")
public class Profile implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@Column(name = "PROFILE_ID",nullable=false,unique=true)
protected int id;

public Profile() {
super();
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Profile other = (Profile) obj;
if (id != other.id)
return false;
return true;
}

}

请帮我解决这个问题

最佳答案

问题的根源在于,包含 tx:annotation-driven 的上下文并未选取您使用 @Transactional 注释进行注释的 bean > 后处理器。你有几个选择。将 @Transactional 移动到由包含 tx:annotation-driven 后处理器的上下文加载的 bean,或移动 tx:annotation-driven code> 后处理器到加载 @Transactional bean 的同一上下文中。

答案here也是类似的情况。

关于java - TransactionrequireException,没有正在进行的交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com