【发布时间】:2015-05-14 14:45:33
【问题描述】:
我正在尝试学习 Spring 框架,并且一直在学习复数视觉教程。
我目前遇到问题,不知道如何解决。 Hibernate 和 JPA 已添加到项目中,它们创建的表很好。本教程的下一部分展示了如何使用实体管理器以及如何将对象保存到数据库中。
我的代码模仿他的,除了我已经更新到新版本的 Spring 并添加了大量 cmets。
我收到的错误是:HTTP Status 500 - Request processing failed; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
在这些 sn-p 中调用、声明和定义实体管理器。
控制器:
@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 实现。
目标服务实现:
@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>
【问题讨论】:
-
你在哪里定义了数据源??
-
@prashantthakre - 由于有大量代码,我已经编辑了问题的详细信息,感谢您查看。
-
看起来你在搞乱你的
dispatcher-config.xml,你能检查一下你有没有像<mvc:annotation-driven/> and <context:component-scan base-package="<correctpathofyourcontroller>"/>这样的正确代码 -
@prashantthakre - 在 servlet-config.mxl 我有 '
' -
@prashantthakre - 我似乎已经修好了,谢谢你的帮助。
标签: java spring hibernate spring-mvc