gpt4 book ai didi

java - CrudRepository 保存方法不返回更新后的实体

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:15 28 4
gpt4 key购买 nike

我正在使用 Spring 并在其中 spring-data-jpa:1.7.0.RELEASEhibernate-jpa-2.1-api:1.0.0.Final。我的数据库是MySQL。在集成测试中,当我保存一个实体时,entityRepository.save() 方法不会返回它的更新版本(例如填充了自动递增的 id),因此我可以看到它在数据库中。

这是我的配置类:

//@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "core.repository")
@Configuration
@PropertySource("classpath:application.properties")
public class JPAConfiguration {

@Autowired
private Environment env;

private
@Value("${db.driverClassName}")
String driverClassName;
private
@Value("${db.url}")
String url;
private
@Value("${db.username}")
String user;
private
@Value("${db.password}")
String password;

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();

}

@Bean
public DataSource dataSource() {

DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect"));

final LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();

em.setJpaVendorAdapter(vendorAdapter);
em.setPackagesToScan(new String[]{"core.persistence"});
em.setDataSource(dataSource());
em.afterPropertiesSet();


return em;
}

@Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}

@Bean
public JpaTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
}

这是我的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JPAConfiguration.class})
//@Transactional
//@TransactionConfiguration(defaultRollback = true)
public class AgencyRepositoryIntegrationTests {

@Autowired
AgencyRepository agencyRepository;

@Test
public void testThatAgencyIsSavedIntoRepoWorks() throws Exception {

Agency c = DomainTestData.getAgency();
Agency d = agencyRepository.save(c);
// here d equals to c but both have id 0.

Collection<Agency> results = Lists.newArrayList(agencyRepository.findAll());
//here results != null and it does contain the agency object.
assertNotNull(results);
}
}

由于研究了 stackoverflow 中的其他问题,我最终评论了 Transactional 注释(在测试和配置类中),但它没有用。

谢谢

最佳答案

尝试取消注释您的@Transactional 注释并运行。我在你的代码片段中看不到任何明显的问题,但我内心有一些东西(这不是饥饿)说你的事务划分不正确。我以前遇到过类似的问题,只是缺少@transaction 注释或事务配置错误。

关于java - CrudRepository 保存方法不返回更新后的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898909/

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