[英]Best way to save and update entity Parent and child using spring data jpa
[英]Can not save/update entity using JPA in spring
我在应用程序中使用JPA,并且在查询对象后即可使用,但是会抛出错误javax.persistence.TransactionRequiredException: No transactional EntityManager available
一旦尝试保存或更新对象,就javax.persistence.TransactionRequiredException: No transactional EntityManager available
。
这是java配置:
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@PropertySource("classpath:dao.properties")
public class JpaConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
.....................
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", ...........)
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.setPackagesToScan("com....");
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
注意我在@EnableTransactionManagement
使用proxyTargetClass = true
,因为我不想在应用程序中创建无用的接口。
这是dao的具体实现:
@Transactional
@Repository
public abstract class AbstractJPADao<I, E> {
@Autowired
@PersistenceContext
protected EntityManager entityManager;
private Class<E> type;
public AbstractJPADao() {
type=....
}
@Override
public Result<E> find(I id) {
E e = entityManager.find(type, id);
return Result.newInstance().setContent(e);
}
@Override
public Result<E> find(Map<String, Object> condition) {
Query q = entityManager.createQuery(".......));
return Result.newInstance().setContent(q.getResultList());
}
@Override
public E save(E element) {
entityManager.persist(element);
return element;
}
@Override
public E update(E element) {
entityManager.merge(element);
return element;
}
@Override
public void delete(E element) {
entityManager.remove(element);
}
}
@Repository
@Transactional
public class DepartmentDao extends AbstractJPADao<String, Department> {
@Override
protected String selectCause(Map<String, Object> condition) {
return "";
}
}
并将控制器作为dao的客户端:
@Controller
@RequestMapping("/api/deps")
public class DepartmentCtrl {
@Autowired
private DepartmentDao departmentDao;
@RequestMapping(value = "", method = RequestMethod.POST)
public Result create(@Valid Department department, BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
departmentDao.save(department);
return Result.newInstance().setContent(department);
}
throw new RuntimeException("...");
}
}
有什么问题吗?
dao.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/proj
jdbc.username=root
jdbc.password=
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
#hibernate.ejb.naming_strategy=true
hibernate.show_sql=true
hibernate.format_sql=true
尝试重命名方法transactionManager
以txManager
在类JpaConfig
自动装配的名称为txManager
编辑
同样,框架可能期望txManager使用无参数方法。 你可以尝试更改为
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
正如Tiny所注意到的,您在AbstractJPADao中的EntityManager类型的受保护字段上具有两个注释@Autowired和@PersistenceContext。 尝试删除@Autowired。 @PersistenceContext足以注入EntityManager。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.