gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:33:26 24 4
gpt4 key购买 nike

我已经解决了很多类似的问题,通常答案是将 @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/

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