【发布时间】:2016-02-20 07:12:07
【问题描述】:
没有可用于当前线程的具有实际事务的 EntityManager - 无法可靠地处理“持久”调用
当我使用 JUnit 进行测试时,persist 方法有效,并且我看到我的对象已插入,但是当我通过控制器调用该方法时不起作用
这是我的项目:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.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-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- <bean id="notification" class="com.app.sqli.notification.NotificationTask" /> -->
<!-- <task:scheduled-tasks> -->
<!-- <task:scheduled ref="notification" method="notifier" cron="*/2 * * * * *"/> -->
<!-- </task:scheduled-tasks> -->
<context:component-scan base-package="com.app.sqli" />
<mvc:annotation-driven />
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.app.sqli.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/sqli" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<tx:annotation-driven />
</beans>
我的模型类:
package com.app.sqli.models;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Collaborateur {
@Id
private int id;
private String nom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
}
我的 DAO 课程
package com.app.sqli.dao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.app.sqli.models.Collaborateur;
@Repository
public class CollaborateurDao implements IcollaborateurDao{
@PersistenceContext
private EntityManager em;
@Override
public void addCollaborateur(Collaborateur c) {
em.persist(c);
}
}
我的服务类
package com.app.sqli.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.app.sqli.dao.IcollaborateurDao;
import com.app.sqli.models.Collaborateur;
@Service
@Transactional
public class CollaborateurService implements IcollaborateurService{
@Autowired
private IcollaborateurDao cdao;
@Override
public void addCollaborateur(Collaborateur c) {
cdao.addCollaborateur(c);
}
}
还有我的控制器
package com.app.sqli.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.app.sqli.models.Collaborateur;
import com.app.sqli.services.IcollaborateurService;
@org.springframework.stereotype.Controller
public class Controller {
@Autowired
private IcollaborateurService cserv;
@RequestMapping(value = "/index")
public String index(Model m) {
System.out.println("insertion ...");
Collaborateur c = new Collaborateur();
c.setId(11);
c.setNom("nom");
cserv.addCollaborateur(c);
return "index";
}
}
【问题讨论】:
-
您遇到的异常是什么?在不知道异常的情况下,我唯一能想到的就是在您的 EM 上使用 PersistenceContextType.Extended。 @PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager em;
-
我得到的错误:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 javax.persistence.TransactionRequiredException: No EntityManager 与当前线程的实际事务可用 - 无法可靠地处理“持久”调用
-
您尝试过 PersistanceContextType.Extended 吗?简而言之,这从事务范围的 EM 变为有状态的会话 bean...
-
它有待解决,现在我有这个错误消息:未知实体:com.app.sqli.models.Collaborateur
-
PackagesToScan 说你的实体应该在这个包 com.app.sqli.entities 但你的实体在:com.app.sqli.models... 他们必须匹配所以将你的实体移动到正确的包或更改 packagesToScan 以匹配您的实体的包
标签: spring-mvc jpa entitymanager