Java Transactionrequiredexception,没有正在进行的事务

Java Transactionrequiredexception,没有正在进行的事务,java,spring,hibernate,spring-mvc,jpa,Java,Spring,Hibernate,Spring Mvc,Jpa,我目前正在从事一个Maven+Spring+Hibernate项目。实际上,这只是一个测试项目,只是为了熟悉Spring如何使用Hibernate(+Maven)。我已经设置并准备了必要的依赖项。i、 e.Spring的appcontext.xml,Hibernate的persistence.xml,JPA/persistence/Hibernate的实体和DAO对象 在调试期间,persist方法抛出Transactionrequiredexception,没有正在进行的事务。我不知道是什么原

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

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

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>
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();
}
}
Profile.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;
}

}

请帮助我解决此问题。问题的根源在于,您用
@Transactional
注释注释的bean没有被包含
tx:annotation-driven
后处理器的上下文拾取。你有几个选择。将
@Transactional
移动到包含
tx:annotation-driven
后处理器的上下文加载的bean中,或将
tx:annotation-driven
后处理器移动到加载
@Transactional
bean的同一上下文中


答案是类似的情况

我是初学者。。你能用我的例子解释一下吗?我试着把@Transactional移到ProfileDao的注释中。。但是没有成功。。不过,我还是得到了同样的例外,因为它奏效了。。除此之外,我还导入了javax.persistence.Transaction包,而不是import org.springframework.Transaction.annotation.Transactional;
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";
}
}
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();
}
}
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;
}

}