spring事务,折腾三天了,找不到原因。请各位高手过过目

lilei_new 2017-07-14 02:16:24
所用框架
spring 4.3.9
dataSource durid
H2
JPA

简单的一个测试程序,按书上的例程来的。不知道什么原因,只要调service里的persist方法(委托给repository的persist方法)就会出错,但是查询却没有问题。

错误
Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:282)
at com.sun.proxy.$Proxy22.persist(Unknown Source)
at com.liyuhao.study.springJpaHibernateDruidSqlite.RepositoryImpl.persist(RepositoryImpl.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)

Spring配置


@Configuration
@ComponentScan(basePackages={"com.liyuhao.study.springJpaHibernateDruidSqlite"})
public class springCfg {

@Bean
public BeanPostProcessor persistenceTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
public PersistenceAnnotationBeanPostProcessor paPostProcessor(){
return new PersistenceAnnotationBeanPostProcessor();
}


@Bean
public LocalContainerEntityManagerFactoryBean getLocalContainerEntityManagerFactoryBean(){
LocalContainerEntityManagerFactoryBean temp=new LocalContainerEntityManagerFactoryBean();

temp.setDataSource(getDataResource());
temp.setJpaVendorAdapter(getHibernateJpaVendorAdapter());
temp.setPackagesToScan("com.liyuhao.study.springJpaHibernateDruidSqlite");
Properties ps=new Properties();
ps.setProperty("hibernate.format_sql", "true");
ps.setProperty("hibernate.hbm2ddl.auto","create");
temp.setJpaProperties(ps);

return temp;
}

@Bean
public HibernateJpaVendorAdapter getHibernateJpaVendorAdapter(){

HibernateJpaVendorAdapter adapter=new HibernateJpaVendorAdapter();

adapter.setDatabase(Database.H2);
adapter.setShowSql(true);
adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect");
adapter.setGenerateDdl(true);

return adapter;
}

@Bean
public DataSource getDataResource(){

DruidDataSource druid=new DruidDataSource();
druid.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false");
druid.setDriverClassName("org.h2.Driver");
//druid.setInitialSize(1);
//druid.setMaxActive(1);
return druid;
/*
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
*/
}
}


...全文
225 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-07-14
  • 打赏
  • 举报
回复
No EntityManager with actual transaction available for current thread 持久化操作加上事务吧。。。@Transactional

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧