【发布时间】:2015-03-31 21:01:08
【问题描述】:
我有一个独立的应用程序,我试图在其中填充数据库。 我的应用程序配置如下:
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:classpath property file" })
@ComponentScan(basePackages = { my packages to scan })
public class PersistenceJPAConfig {
@Bean
public EntityManagerFactory entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { packages to scan});
JpaVendorAdapter vendorAdapter = jpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
em.afterPropertiesSet();
return em.getObject();
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.ORACLE);
return jpaVendorAdapter;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl(url);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
return new Properties() {
{ // Hibernate Specific:
setProperty("hibernate.hbm2ddl.auto", "validate");
setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
}
};
}
}
但是,当我尝试在数据库中保留某些内容时,注释失败并出现错误: 线程“主”javax.persistence.TransactionRequiredException 中的异常:没有可用的事务性 EntityManager
在生成的错误堆栈中,我没有看到正在应用的事务建议。
包含事务注解的文件是这样的:
import org.springframework.transaction.annotation.Transactional;
@Service
public class MyService {
@Autowired
private MyDao myDao;
@Transactional
public void save(MyEntity e) {
myDao.save(e);
}
}
MyDao 类是这样的:
@Repository
public class MyDao {
@PersistenceContext
private EntityManager entityManager;
public void save(MyEntity e) {
entityManager.persist(e);
}
}
我从我的 Main 方法调用该函数,如下所示:
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
ctx.scan(CONFIG_PACKAGE);
ctx.refresh();
myService = ctx.getBean(MyService.class);
Thread.sleep(10 * 1000);
myService.save(entity);
...
}
堆栈跟踪看起来像这样(经过编辑以删除与我的代码有关的太具体的跟踪):
Exception in thread "main" javax.persistence.TransactionRequiredException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
at com.sun.proxy.$Proxy27.persist(Unknown Source)
at mypackage.MyDao.save(MyDao.java:42)
at mypackage.MyDao$$FastClassBySpringCGLIB$$758201c8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at mypackage.MyDao$$EnhancerBySpringCGLIB$$9bd3b4fc.save(<generated>)
at mypackage.MyService.save(MyService.java:176)
at mypackage.MyService$$FastClassBySpringCGLIB$$5c0a03f6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
at mypackage.Run.main(Run.java:109)
谁能帮我解决这个问题,即我可能做错了什么? 感谢您的帮助
【问题讨论】:
-
你能发布完整的堆栈跟踪吗?另外,
entity在您的main方法中来自哪里? -
添加了堆栈跟踪。我在保存之前在 main 方法本身中创建实体对象。
-
您是否尝试过在您的
EntityManager声明中使用@Autowired而不是@PersistenceContext? -
试过了,没用。