gpt4 book ai didi

java - Spring ORM JNDI Tomcat - 没有可用的实际事务的 EntityManager

转载 作者:行者123 更新时间:2023-11-28 23:28:54 24 4
gpt4 key购买 nike

我可以找到但无法保存我的实体。

Tomcat 上下文:

      <Resource name="jdbc/spring" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="spring" password="spring" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/spitter"/>

DatabaseConfig 和 jpa 配置:

@Configuration
public class DatabaseConfig {

@Bean
public JndiObjectFactoryBean dataSource(){
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/spring");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return jndiObjectFactoryBean;
}

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

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter adapter){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(adapter);
entityManagerFactoryBean.setPackagesToScan("com.springinaction.spittr");
entityManagerFactoryBean.setJpaProperties(jpaProperties());
return entityManagerFactoryBean;
}

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

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

private Properties jpaProperties(){
Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto", "create-drop");
return properties;
}

和 DAO:

@Repository
@Transactional
public class UserRepositoryImpl implements UserRepository {

@PersistenceContext
private EntityManager entityManager;

@Override
public User findUser(Long id) {
return entityManager.find(User.class, id);
}

@Override
public User findUserByUsername(String username) {
return (User)entityManager.createQuery("Select u from User u where username= :username ")
.setParameter("username", username)
.getSingleResult();
}

@Override
public void addUser(User user) {
entityManager.persist(user);
}
}

服务:

@Service
public class UserService implements UserDetailsService {

@Autowired
private UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
User user = new User();
user.setUsername("admin");
user.setPassword("admin");
Role role = new Role();
role.setRole("ADMIN");
Role role2 = new Role();
role2.setRole("USER");
HashSet set = new HashSet();
set.add(role);
set.add(role2);
user.setRoles(set);
userRepository.addUser(user);
return userRepository.findUserByUsername(s);
}
}

当我到达 userRepository.addUser(user) 时,没有实体被保存到我的数据库中,我得到没有 EntityManager 和实际事务可用异常。

我哪里错了?

最佳答案

好的,我没有将@EnableTransactionManagement() 放在我的配置上。关闭

关于java - Spring ORM JNDI Tomcat - 没有可用的实际事务的 EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412021/

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