gpt4 book ai didi

java - Spring TransactionRequiredException 异常

转载 作者:行者123 更新时间:2023-11-29 06:56:56 26 4
gpt4 key购买 nike

这是我的存储库配置:

@Configuration
public class RepositoryConfing {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactoryBean.setPackagesToScan("com.imdb.model");
return entityManagerFactoryBean;
}

@Bean
public BasicDataSource dataSource(){
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl("jdbc:postgresql://localhost:5432/imdb");
ds.setUsername("***");
ds.setPassword("***");
ds.setInitialSize(5);
return ds;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter(){
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.POSTGRESQL);
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
return adapter;
}
}

当我调用 merge 方法时,我得到一个异常:javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliable process 'merge' call

可能是我的 RepositoryConfig 缺少一些额外的配置?

编辑:我的新存储库配置:

@Configuration
@EnableTransactionManagement
public class RepositoryConfing {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactoryBean.setPackagesToScan("com.imdb.model");
return entityManagerFactoryBean;
}

@Bean
public BasicDataSource dataSource(){
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl("jdbc:postgresql://localhost:5432/imdb");
ds.setUsername("***");
ds.setPassword("***");
ds.setInitialSize(5);
return ds;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter(){
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.POSTGRESQL);
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
return adapter;
}

@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource());
}
}

现在它因 StackOverflow 而失败。可能是 DataSourceTransactionManager 不适合 JPA?

最佳答案

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
  • 您的 txManager 方法应重命名为 transactionManager
  • 您需要一个 JpaTransactionManager 而不是 DataSourceTransactionManager

编辑

为了与 JPA 进行功能齐全的交易,您需要:

  • 在您的配置文件中使用@EnableTransactionManagement
  • 声明一个EntityManagerFactoryBean
  • 声明一个 JpaTransactionManager(bean 必须命名为 transactionManager),它是使用您之前声明的 EntityManagerFactoryBean
  • 创建的
  • 在您的@Repository(必须是一颗 Spring Bean )
  • @Service 调用您的存储库并使用带有 @Transactional 注释的 public 方法

当然这是简化的,我假设您正在使用 java 配置、注释和自动组件扫描。

关于java - Spring TransactionRequiredException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com