简体   繁体   中英

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/

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). 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). Usually service methods are the place to put @Transactional (some people put it on DAO methods, but that's unnecessarily granular)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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