我可以找到,但我无法保存我的实体。
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 config:
@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(用户)时,没有实体保存到我的数据库中,并且我得到了没有实体事务可用异常的EntityManager。
我哪里错了?
答案 0 :(得分:0)
好的,我没有在我的配置上放置@EnableTransactionManagement()。 关闭