![](/img/trans.png)
[英]Spring 4 + JPA (Hibernate 4) + JTA transaction manager doesn't flush automatically
[英]Transaction Entity Manager not available Spring/JPA/Hibernate - Pluralsight
我正在嘗試學習Spring框架,並一直在完成復數視覺教程。
我目前遇到問題,不確定如何解決。 Hibernate和JPA已添加到項目中,它們可以很好地創建表。 本教程的下一部分顯示了如何使用實體管理器以及如何將對象保存到數據庫。
我的代碼模仿了他,除了我已更新到較新版本的Spring並添加了大量注釋。
我收到的錯誤是: HTTP Status 500 - Request processing failed; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
HTTP Status 500 - Request processing failed; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
在這些代碼段中將調用,聲明和定義實體管理器。
控制器:
@Controller
@SessionAttributes("goal")
public class GoalController {
@Autowired
private GoalService goalService;
@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {
...
goalService.save(goal);
return "redirect:index.jsp";
}
}
GoalService是具有簡單Goal save(Goal goal);
然后由GoalServiceImpl實現。
GoalServiceImpl:
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Override
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
GoalRepository具有相同的模式。
GoalrespositoryImpl:
@Repository("goalRepository")
公共類GoalRepositoryImpl實現GoalRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public Goal save(Goal goal) {
entityManager.persist(goal);
return goal;
}
}
我回過頭看了視頻,看不到哪里出了問題,我在調試中逐步進行了調試,但是看不到任何明顯的東西,即使是我也可能看不到。
有人可以照亮嗎?
非常感謝。
編輯:如果我注釋掉實體管理,它運行正常,顯然不會得到保存,但不會通過異常。
@Override
public Goal save(Goal goal) {
//entityManager.persist(goal);
return null;
}
編輯-數據源:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.hbm2ddl.auto" value="create"/>
<entry key="hibernate.format_sql" value="true"/>
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<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/fitnessTracker?autoReconnect=true"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
這可能是fl幸,我不確定...
我搬了
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>`
從jpaContext.xml進入servlet-config.xml。 我發現很少有類似的帖子暗示它們應該與組件掃描所在的類處於同一類。
雖然我仍然遇到問題,但我認為這不是整體解決方案。
然后,我發現引用了@Transactional批注的隨機帖子。 我添加了此@Override @Transactional public Goal save(Goal goal) { return goalRepository.save(goal); }
@Override @Transactional public Goal save(Goal goal) { return goalRepository.save(goal); }
和瞧..它的工作原理。 不管是否應該完全是另一回事,希望這對那些偶然發現它的人也有所幫助,如果他們也正在學習本教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.