Spring + Hibernate - 持久/提交数据不起作用

时间:2011-04-26 19:09:03

标签: java hibernate spring

我是一对一的指南代码“一篇文章这篇文章必须意味着很多代码和配置”。

http://blog.springsource.com/2006/08/07/using-jpa-in-spring-without-referencing-spring/

问题是,只有类似选择的查询才有效。当我尝试持久化bean /实体时,查询就不会发生(我已经为Hibernate设置了show sql选项)。我知道这可能与弹簧配置有关,但我没有找到要寻找的经验。

Spring配置:

    <?xml version="1.0" encoding="UTF-8"?>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" />

<bean id="productDaoImpl" class="product.ProductDaoImpl"/>

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

Dao:

    @Repository
public class ProductDaoImpl implements ProductDao {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this. entityManager = entityManager;
    }

    // works
    public Collection loadProductsByCategory(String category) {
        return entityManager.createQuery("from Product p where p.category = :category")
            .setParameter("category", category).getResultList();
    }

   // Doesn't even get queried for
   public void persistWhatever(Product product) { entityManger.persist(product); }
}

1 个答案:

答案 0 :(得分:2)

我猜你的服务方法(那些调用DAO)没有@Transactional。通常服务方法是放置@Transactional的地方(有些人把它放在DAO方法上,但那是不必要的粒度)