|
|
Hi,
I create a small test to check the merge functionality.
I basically:
1) retreive in one session (via JpaTemplate) an object.
2) Modify the object
3) from another session (still via JpaTemplate) merge the object.
When I check the database the object is not changed?
Here is a part of the code:Code:
public void testUpdateColorStone() {
System.out.println(quot;sssss START ssssquot;);
TestObject testObject = (TestObject) testDAO.findByReference(quot;Testquot;);
System.out.println(testObject .getComments());
testObject .setComments(quot;last commentsquot;);
System.out.println(testObject .getComments());
testDAO.merge(testObject );
}
I don't see in the log file any update statement!
My DAO use the entityManager to create a session.
[main] DEBUG org..orm.jpa.JpaTemplate - Creating new EntityManager for JpaTemplate execution
[main] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 4743565661175808
When I use the merge in the same session/transaction as the finder, it works but not with different session. Isn't it the goal of the merge to reattach an object and save it?
Any Idea?
Thanks
Dominique
Yes it is - make sure that both sessions are read-write and the transaction in which they are wrapped commits properly. If that still doesn't work and the Hibernate logs don't provide any help, go try their forums.
The HibernateEM hasn't been updated in a while and there are a number of bug fixes pending.
Thanks,
My sessions are in read-write and commit, because I'm able to insert new object.
I'll make a test in pure Hibernate (without spring) to verify it.
Thanks for your advice.
Dominique
mit();
em.close();
em = emf.createEntityManager();
System.out.println(testObject.getComments());
testObject.setComments(quot;last comments4quot;);
System.out.println(testObject.getComments());
tx = em.getTransaction();
tx.begin();
em.merge(testObject);
tx.commit();
em.close();
}
so then there is an issue with the config of JpaTemplate here is my JpaTemplate bean definiationCode:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:aop=quot;schema/aopquot; xmlns:tx=quot;schema/txquot; xsi:schemaLocation=quot; schema/beans schema/beans/spring-beans.xsd schema/tx schema/tx/spring-tx.xsd schema/aop schema/aop/spring-aop.xsdquot;gt;
lt;bean id=quot;entityManagerFactoryquot; class=quot;org..orm.jpa.LocalEntityManagerFactoryBeanquot;gt;
lt;property name=quot;persistenceUnitNamequot; value=quot;modelquot;/gt;
lt;/beangt;
lt;bean id=quot;txManagerquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt;
lt;aop:configgt; lt;aop:pointcut id=quot;productServiceMethodsquot; expression=quot;execution(* test.*.*.*.*(..))quot;/gt; lt;aop:advisor advice-ref=quot;txAdvicequot; pointcut-ref=quot;productServiceMethodsquot;/gt; lt;/aop:configgt;
lt;tx:advice id=quot;txAdvicequot; transaction-manager=quot;txManagerquot;gt; lt;tx:attributesgt; lt;tx:method name=quot;*quot; propagation=quot;REQUIRES_NEWquot;/gt; lt;/tx:attributesgt;
lt;/tx:advicegt;
lt;/beansgt;
With JpaTemplate I have in the log:
main] DEBUG org.hibernate.loader.Loader - done entity load
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org..orm.jpa.JpaTemplate - Closing new EntityManager after JPA template execution
[main] DEBUG org..beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[main] DEBUG org..beans.factory.support.DefaultL istableBeanFactory - Bean with name 'entityManagerFactory' is a factory bean
[main] INFO org.hibernate.impl.SessionFactoryImpl - closing
[main] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - cleaning up connection pool: jdbc:mysql--127.0.0.1/gemsWith EM I have:
[main] DEBUG org.hibernate.loader.Loader - done entity load
[main] DEBUG org.hibernate.transaction.JDBCTransaction - commit
[main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - processing flush-time cascades
[main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - dirty checking collections
[main] DEBUG org.hibernate.engine.Collections - Collection found: [test.entity.model.TestObject.treatments#402881830d a37baf010da37bb6540005], was: [test.entity.model.TestObject.treatments#402881830d a37baf010da37bb6540005] (initialized)
[main] DEBUG org.hibernate.engine.Collections - Collection found: [test.entity.model.TestObject.certificates#40288183 0da37baf010da37bb6540005], was: [test.entity.model.TestObject.certificates#40288183 0da37baf010da37bb6540005] (uninitialized)
[main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - Flushed: 0 insertions, 1 updates, 0 deletions to 6 objects
[main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - Flushed: 0 (re)creations, 0 updates, 0 removals to 2 collections
[main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[main] DEBUG org.hibernate.SQL - update TestObject set reference=? where id=?
Hibernate: update TestObject set reference where id=?
[main] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
[main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[main] DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit
[main] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
[main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[main] DEBUG org..beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[main] DEBUG org..beans.factory.support.DefaultL istableBeanFactory - Bean with name 'entityManagerFactory' is a factory bean
[main] INFO org.hibernate.impl.SessionFactoryImpl - closing
[main] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - cleaning up connection pool: jdbc:mysql--127.0.0.1/gems
It seems that some Listener are not started or triggered with the JpaTemplate!
If somebody has any idea, it will be welcome.
Thanks
Dominique
I found the problem, my aop:config expression wasn't correct and then no transaction was created around the merge method which was a method in the super class of my stoneDAO.
Now it works, thanks for your advise Costin. |
|