在春天无法使用JPA保存/更新实体

hgu*_*ser 10 java spring jpa spring-data-jpa

我在我的应用程序中使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意我proxyTargetClass = true在中使用 @EnableTransactionManagement,因为我不想在我的应用程序中创建无用的接口.

这是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 "";
    }
}
Run Code Online (Sandbox Code Playgroud)

而控制器作为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("...");
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么不对的吗?


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
Run Code Online (Sandbox Code Playgroud)

gip*_*psy 4

尝试将方法重命名transactionManagertxManager类中的方法JpaConfig

自动装配的名称为txManager

编辑

此外,框架可能需要 txManager 的无参数方法。你可以尝试更改为

 @Bean
 public PlatformTransactionManager transactionManager() {
     JpaTransactionManager transactionManager = new JpaTransactionManager();
     transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
     return transactionManager;
 }
Run Code Online (Sandbox Code Playgroud)