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

标签 java spring hibernate jpa

我已经解决了很多类似的问题,通常答案是将 @Transactional 添加到类和方法中。但这对我不起作用,因此我认为我做错了什么?

我的存储库:

@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 (代码片段):

@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" /> 

关于java - Spring Web App + JPA 没有可用的事务性 EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39207592/

相关文章:

java - Hibernate 无法解析属性

hibernate - JSR-303 依赖注入(inject)和 Hibernate

java - JSoup - 如何获取紧接在 <span class = *> 之前的 href(url/link)?

java - 旋转僵尸面对玩家

spring - Dockerfile 无法构建 jar 文件 : Main class name has not been configured

spring - 为什么我的 spring security 配置似乎拒绝有效的 csrf token

java - 从应用程序代码获取 hibernate 映射

java - 限制对 AppEngine 上的 JRE 库类的访问的目的是什么?

java - Oracle 的 DMS 类文件在哪里?

spring - 如何使用 spring security 2.0 在我的 JSP 页面中显示错误消息