gpt4 book ai didi

java - Spring 无法使用 JPA 保存/更新实体

转载 作者:IT老高 更新时间:2023-10-28 13:50:40 25 4
gpt4 key购买 nike

我在我的应用程序中使用 JPA,一旦我查询对象,它就可以工作,但是一旦我尝试保存或更新对象,它就会抛出错误 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,因为我不想在我的应用程序中创建无用的接口(interface)。

这是道的具体实现:

@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 "";
}
}

而 Controller 作为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

最佳答案

尝试在类 JpaConfig 中将方法 transactionManager 重命名为 txManager

Autowiring 的名字是 txManager

编辑

此外,框架可能期望 txManager 的无参数方法。你可以试试改成

 @Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}

关于java - Spring 无法使用 JPA 保存/更新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556457/

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