gpt4 book ai didi

java - 无法解决错误 "save is not valid without active transaction"

转载 作者:行者123 更新时间:2023-12-02 12:10:03 26 4
gpt4 key购买 nike

我无法解决错误

save is not valid without active transaction

我的代码:

public class PersistenceJPAConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "self.experiment.dto" });

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
return hemf.getSessionFactory();
}

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

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}

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

Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.current_session_context_class", "thread");
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}

public class UserBL {

@Autowired
UserDAO userDao;

@Transactional(transactionManager="transactionManager")
public void saveUsers(){
User user = new User();
user.setName("Bob");
userDao.insert(user);
}
}

在很多博客上,我读到从 JPA 属性中删除 hibernate.current_session_context_class 将解决此问题。但是当我删除它时,我收到以下错误

No CurrentSessionContext configured!

最佳答案

您正在配置 SessionFactory 和 EntityManagerFactory。删除所有 Hibernate 特定的内容:

@Configuration
public class PersistenceJPAConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "self.experiment.dto" });

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

return em;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("root");

return dataSource;
}

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

return transactionManager;
}

Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}

服务

public class UserBL {

@Autowired
UserDAO userDao;

@Transactional
public void saveUsers(){
User user = new User();
user.setName("Bob");

userDao.insert(user);
}
}

DAO

public class UserDAO {
@PersistenceContext
private EntityManager em;
}

关于java - 无法解决错误 "save is not valid without active transaction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609466/

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