-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: changes are not stored
PostPosted: Mon May 14, 2007 5:17 pm 
Newbie

Joined: Sat May 05, 2007 5:42 am
Posts: 4
I am running hibernate with spring and have some trouble with saving my changes persistent.
My class is a litte sequence generator and shall increment the nextValue in a transaction.
But whenever the application start, the same initial values are used. The code works, the
sequence is incremented, but changes are not written to database. Ich think the changes were
not written ti database, but only to the hibernate cache.

My unit test code:

Code:
      ClassPathResource res = new ClassPathResource("spring.xml");
      XmlBeanFactory factory = new XmlBeanFactory(res);

      SequenceDaoImpl cl = (SequenceDaoImpl) factory.getBean("fooService");
      for (int idx = 0; idx < 5; idx++) {
         long val = cl.nextValue("hallo");
         System.out.println("nextValue=" + val );
      }


Spring configuration:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <!-- this is the service object that we want to make transactional -->
  <bean id="fooService" class="de.bytefusion.cashflow.core.sequence.SequenceDaoImpl">
   <property name="sessionFactory" ref="sessionFactory"/>
   <property name="transactionManager" ref="transactionManager"/>
  </bean>

  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven/>

  <!-- a PlatformTransactionManager is still required -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
   
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
  </bean>   

</beans>      

And my service implementation:
Code:
...
import org.springframework.transaction.annotation.Transactional;
...

@Transactional
public class SequenceDaoImpl implements ISequence {

   private HibernateTemplate hibernateTemplate;
   private PlatformTransactionManager transactionManager;
   
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
   
   public PlatformTransactionManager getTransactionManager() {
      return transactionManager;
   }

   public void setTransactionManager(PlatformTransactionManager transactionManager) {
      this.transactionManager = transactionManager;
   }

   @Transactional(propagation=Propagation.REQUIRES_NEW)
   public long nextValue(String name) throws ToolsException {
      long nextId = 0;
      Sequence seq = (Sequence) hibernateTemplate.get(Sequence.class, name);
      if ( seq == null ) {
         seq = new Sequence();
         seq.setName(name);
         seq.setNextValue(1);
         hibernateTemplate.save(name, seq);
      } else {
         nextId = seq.getNextValue();
         seq.setNextValue(nextId+1);
         hibernateTemplate.update(seq);
      }

      return nextId;
   }

}


What's wrong?

Thx, markus


Top
 Profile  
 
 Post subject: extracted pure hibernate
PostPosted: Tue May 15, 2007 11:32 am 
Newbie

Joined: Sat May 05, 2007 5:42 am
Posts: 4
I reduced the problem to pure hibernate and remove any spring reference:

Code:
      AnnotationConfiguration cfg = new AnnotationConfiguration();
      cfg.configure("hibernate.cfg.xml");
      fac = cfg.buildSessionFactory();
      
      Transaction tx = fac.openSession().beginTransaction();
      SequenceDaoImpl cl = (SequenceDaoImpl) factory.getBean("fooService");
      for (int idx = 0; idx < 5; idx++) {
         long val = cl.nextValue("hallo");
         System.out.println("nextValue=" + val );
      }
      tx.commit();


The initial value is read from apache derby database, but any changes are only visible within the application. There are no physical changes to the database. I may change initial value by using another sql-client, so that the app see's other values, but the effect is the same: no changes are written.

Derby version = 10.2.2.0
Hibernate3 version = 3.2.3.ga
Java 1.6

regards,
markus


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.