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

时间:2015-09-14 02:16:50

标签: 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;
    }
}

注意我在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 "";
    }
}

并且控制器作为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

2 个答案:

答案 0 :(得分:4)

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

自动装配名称为 txManager

<强> 修改

此外,框架可能期望txManager的无参数方法。你能尝试换到

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

答案 1 :(得分:1)

正如Tiny注意到的那样,在AbstractJPADao中的EntityManager类型的受保护字段上有两个@Autowired和@PersistenceContext注释。 尝试删除@Autowired。 @PersistenceContext足以注入EntityManager。