jpa hibernate @transactional 配置
jpa hibernate @transactional configuration
如果没有 @Transactional
,如果我的 Web 应用程序可以运行,但当它调用删除时我会收到以下异常:
Request processing failed; nested exception is
javax.persistence.TransactionRequiredException: No transactional
EntityManager available
所以我想让它成为事务性的,但是如果我将 @Transactional
添加到 class 或方法中,当我 运行 我得到的应用程序时:
No qualifying bean of type [net.tirasa.jpaaddressbook.JpaEntryDAO]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency.
我的 JpaEntryDAO 是:
@Repository
public class JpaEntryDAO implements EntryDAO {
@PersistenceContext
@Autowired
private EntityManager entityManager;
@Override
@Transactional // <<<<<<------- add or remove it
public void remove(int id) {
Entry entry = new Entry();
entityManager.remove(id);
}
//[...]
控制器class:
@Controller
public class AddressBookController {
@Autowired
private JpaEntryDAO dao;
@RequestMapping(value = { "/", "/index" })
public void home() {
}
[...]
applicationContext.xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="net.tirasa.jpaaddressbook/"/>
<context:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!--<bean id="dao" class="net.tirasa.jpaaddressbook.JpaEntryDAO"/>-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/Rubrica" />
<property name="username" value="matt3o" />
<property name="password" value="secret" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/Views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="EntryPU" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="net.tirasa.jpdaaddressbook" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<!--org.springframework.jdbc.datasource.DataSourceTransactionManager-->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
</beans>
找到错误有什么想法吗?这是一个更完整的堆栈跟踪:
Error creating bean with name 'addressBookController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.tirasa.jpaaddressbook.JpaEntryDAO net.tirasa.jpaaddressbook.AddressBookController.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.tirasa.jpaaddressbook.JpaEntryDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
取消注释 xml 中 JpaEntryDAO 类型的 bean。
如果没有 @Transactional
,如果我的 Web 应用程序可以运行,但当它调用删除时我会收到以下异常:
Request processing failed; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available
所以我想让它成为事务性的,但是如果我将 @Transactional
添加到 class 或方法中,当我 运行 我得到的应用程序时:
No qualifying bean of type [net.tirasa.jpaaddressbook.JpaEntryDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
我的 JpaEntryDAO 是:
@Repository
public class JpaEntryDAO implements EntryDAO {
@PersistenceContext
@Autowired
private EntityManager entityManager;
@Override
@Transactional // <<<<<<------- add or remove it
public void remove(int id) {
Entry entry = new Entry();
entityManager.remove(id);
}
//[...]
控制器class:
@Controller
public class AddressBookController {
@Autowired
private JpaEntryDAO dao;
@RequestMapping(value = { "/", "/index" })
public void home() {
}
[...]
applicationContext.xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="net.tirasa.jpaaddressbook/"/>
<context:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!--<bean id="dao" class="net.tirasa.jpaaddressbook.JpaEntryDAO"/>-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/Rubrica" />
<property name="username" value="matt3o" />
<property name="password" value="secret" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/Views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="EntryPU" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="net.tirasa.jpdaaddressbook" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<!--org.springframework.jdbc.datasource.DataSourceTransactionManager-->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
</beans>
找到错误有什么想法吗?这是一个更完整的堆栈跟踪:
Error creating bean with name 'addressBookController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.tirasa.jpaaddressbook.JpaEntryDAO net.tirasa.jpaaddressbook.AddressBookController.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.tirasa.jpaaddressbook.JpaEntryDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
取消注释 xml 中 JpaEntryDAO 类型的 bean。