简体   繁体   English

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

[英]Spring + Hibernate - Persisting/Committing data doesn't work

I'm one on one with the guides code under the section "A post this long must mean a lot of code and configuration". 我是一对一的指南代码在“这个长期的帖子必须意味着很多代码和配置”部分。

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

The problem is, that only select-like queries work. 问题是,只有类似选择的查询才有效。 When i try to persist a bean/entity the query just doesn't happen (i have show sql option set on for Hibernate). 当我尝试持久化bean /实体时,查询就不会发生(我已经为Hibernate设置了show sql选项)。 I know this is propably something related to springs configuration, but i have no experience on what to look for. 我知道这可能是与弹簧配置相关的东西,但我没有找到要寻找的经验。

Spring configuration: 弹簧配置:

    <?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); }
}

I would guess that you don't have @Transactional on your service methods (those calling the DAO). 我猜你的服务方法(调用DAO的那些)没有@Transactional Usually service methods are the place to put @Transactional (some people put it on DAO methods, but that's unnecessarily granular) 通常服务方法是放置@Transactional的地方(有些人把它放在DAO方法上,但这是不必要的粒度)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM