gpt4 book ai didi

Spring Boot - 在 Hibernate 中使用 JPA 存储库

转载 作者:行者123 更新时间:2023-12-03 21:33:59 25 4
gpt4 key购买 nike

我有一个简单的 Spring Boot 应用程序,它具有以下自动配置属性:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mywebapp
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=validate

这些工作正常,我可以设置 Spring Data JpaRepositories :

public interface UserRepository extends JpaRepository<User, String>
{
User findByName(String name);
}

...对于以下实体:

@Entity
public class User
{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
protected String uuid;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String password;

@Column(nullable = false)
private String email;
}

...并像这样使用它们:

@Transactional
public void updatePassword(String username, String password)
{
User user = userRepository.findByName(username);
user.setEmail("test@example.com"); // This gets persisted automatically by the JpaRepository.
}

现在我正在努力手动配置相同的东西。我尝试了以下方法:

@Configuration
@EnableTransactionManagement
public class PersistenceConfig
{
@Bean
public DataSource dataSource()
{
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/mywebapp");
dataSource.setUsername("username");
dataSource.setPassword("password");

return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan("com.example.persistent");

Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
sessionFactoryBean.setHibernateProperties(hibernateProperties);

return sessionFactoryBean;
}

@Bean
public HibernateTransactionManager transactionManager()
{
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}

}

...但是虽然没有抛出任何异常并且我现在可以成功地从数据库中读取,但似乎我对实体所做的任何更改都没有被持久化.

有谁知道我缺少什么才能让坚持发挥作用?

最佳答案

OP在这里。

我似乎误解了 JPA 需要 EntityManager 而不是 session 。

以下配置有效:

@Configuration
@EnableTransactionManagement
public class PersistenceJpaConfig
{
@Bean
public DataSource dataSource()
{
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/mywebapp");
dataSource.setUsername("username");
dataSource.setPassword("password");

return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.example.persistent");

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);

Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
em.setJpaProperties(properties);

return em;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf)
{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);

return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
{
return new PersistenceExceptionTranslationPostProcessor();
}

}

关于Spring Boot - 在 Hibernate 中使用 JPA 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38352029/

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