繁体   English   中英

Spring Web App + JPA没有可用的事务性EntityManager

[英]Spring Web App + JPA No transactional EntityManager available

我在SO上经历了很多类似的问题,通常的答案是将@Transactional添加到类和方法中。 但这对我不起作用,因此我认为我在做其他错误吗?

MyRepository:

@Repository
public class MyRepository extends AbstractRepository<MyEntity> implements org.springframework.data.repository.Repository<MyEntity, Long> {

    @PersistenceContext
    private EntityManager em;

    public MyRepository() {
        super(MyEnitiy.class);
    }

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public List<MyEnitiy> getAllFor(Integer id) {        
        return getEntityManager().createQuery("SELECT k FROM MyEntity k WHERE k.otherid = :otherid ORDER BY k.something", MyEntity.class)
            .setParameter("otherid", id).getResultList();
    }
}

摘要存储库:

@Transactional(readOnly = true)
public abstract class AbstractRepository<T> {

    ...
    @Modifying
    @Transactional
    public void edit(T entity) {
        getEntityManager().merge(entity);
    }
    ...
}

我的Sping Web控制器(摘要):

@Controller
public class MyController {
    @Autowired
    private MyRepository myRepository;

    @RequestMapping(value = {"/here/there"}, method = RequestMethod.GET)
    @ResponseBody
    @Transactional
    public String hereAndTherePage(@RequestParam(value = "id", required = true) Integer id,
            HttpServletRequest request, HttpSession session) {
        List<MyEntity> myEntities = myRepository.getAllFor(id);
        for(MyEntity myEntity : myEntities) {
            ...
            myEntity.setSomeValue(myEntity.getSomeValue() + 1);
            ...
            myRepository.edit(myEntity);

更新:

spring-database.xml(使用池是尝试修复Tomcat内存泄漏的尝试):

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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example.*" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
</bean>

<bean id="poolProperties" class="org.apache.tomcat.jdbc.pool.PoolProperties">
    <property name="url" value="jdbc:mysql://localhost:3306/bfwinkel?noAccessToProcedureBodies=true"/> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnBorrow" value="true"/>
    <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
    <property name="maxActive" value="10"/>
    <property name="maxIdle" value="10"/>
    <property name="username" value="d[-.-]b"/>
    <property name="password" value="~!~"/>
</bean>

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
    <property name="poolProperties" ref="poolProperties"/>
</bean>

该异常被抛出时getEntityManager().merge(entity); 叫做:

javax.persistence.TransactionRequiredException: No transactional EntityManager available

将以下几行添加到您的spring-database.xml中

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

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

此错误意味着,当您尝试合并实体时,它们没有可用的持久性上下文。

您有三个选项可按优先顺序解决此问题。

1-您是否已将@EnableTransactionManagement添加到您的配置中?

2-如果您有一个persistence.xml文件,则应将一个unitname属性添加到@PersistenceContext。

3-使用扩展持久性上下文,而不是事务范围的持久性上下文。

希望我的回答对您有所帮助。

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM