-->
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: Problems with persistent objects in the main() method
PostPosted: Wed Sep 15, 2010 12:08 pm 
Newbie

Joined: Wed Sep 15, 2010 11:55 am
Posts: 1
Hello,

I am a newbie in Hibernate and I have a question.

I divided my application in 2 layers: DAO layer and Service layer.

A class from my DAO Layer:
Code:
public class SchuelerDaoImpl implements SchuelerDao {

   private SessionFactory sessionFactory;

   public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
   }
   
   @Override
   public Schueler persistOrMerge(Schueler schueler) {
      Schueler s = (Schueler) this.sessionFactory.getCurrentSession().merge(schueler);
      return s;
   }
}


A class from my service layer:

Code:
public class SchuelerServiceImpl implements SchuelerService {

   private SchuelerDao schuelerDao;
   
   public void setSchuelerDao(SchuelerDao schuelerDao) {
      this.schuelerDao = schuelerDao;
   }
   
   @Override
   @Transactional
   public Schueler createUser(Schueler schueler) {
      Schueler s = schuelerDao.persistOrMerge(schueler);
      //s.setName("neuer name");
      return s;
   }
}


And my main() method:
Code:
public static void main(String[] args) {
      ApplicationContext beanFactory = new FileSystemXmlApplicationContext("./src/resources/applicationContext.xml");

      SchuelerService schuelerService = (SchuelerService)beanFactory.getBean("schuelerService");
      Schueler mustermann = new Schueler();

      mustermann.setName("mustermann");
      mustermann.setVorname("vorname");
      
      mustermann = schuelerService.createUser(mustermann);
}


As you see, I am using hibernate + spring, and here is my spring configuration, if needed:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       .........

   <tx:annotation-driven/>
   <bean id="transactionManager"
               class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory" ref="sessionFactory"/>
   </bean>

   <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost/db"/>
       <property name="username" value="root"/>
       <property name="password" value=",,,,,"/>
   </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="dataSource" ref="myDataSource"/>
       
       <property name="hibernateProperties">
            <value>
               hibernate.show_sql=true
                hibernate.format_sql=true
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
           </value>
      </property>
      <property name="annotatedClasses">
        <list>
          <value>domain.Schueler</value>
        </list>
      </property>
   </bean>


   
   <bean id="schuelerDao" class="dao.impl.SchuelerDaoImpl">
       <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
   
   <bean id="schuelerService" class="service.impl.SchuelerServiceImpl">
      <property name="schuelerDao" ref="schuelerDao" />
   </bean>

</beans>



I read that hibernate objects are persistent, when loaded from the DB for example, and that they are automatically updated, so I tried in my service layer:

Code:
@Override
   @Transactional
   public Schueler createUser(Schueler schueler) {
      Schueler s = schuelerDao.persistOrMerge(schueler);
      s.setName("neuer name");
      return s;
   }

And yes, it saves the entry with the new name, "neuer name". So seems to work.
But when I try the same thing again, in the main(String[] args) method, using this SAME OBJECT, it doesn't update the database:

Code:
public static void main(String[] args) {
      ApplicationContext beanFactory = new FileSystemXmlApplicationContext("./src/resources/applicationContext.xml");

      SchuelerService schuelerService = (SchuelerService)beanFactory.getBean("schuelerService");
      Schueler mustermann = new Schueler();

      mustermann.setName("mustermann");
      mustermann.setVorname("vorname");
      
      mustermann = schuelerService.createUser(mustermann);
      mustermann.setName("new name 2");
}


Why? It is using the same object used in createUser(Schueler schueler), and there it works.. why doesn't that work here?


Thanks!

(The persistent object is domain.Schueler)


Top
 Profile  
 
 Post subject: Re: Problems with persistent objects in the main() method
PostPosted: Wed Sep 15, 2010 10:13 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
Hibernate persistent objects works within a scope/boundary context, and in your case in a "@Transaction" in your service method as you declared/demarcated it. When you tried to set the name second time in main, you are outside service method, which mean the transaction has already ended, and therefore will not update your DB.

Read http://community.jboss.org/wiki/Sessionsandtransactions, and the Hibernate ref manual.

And since you are using Spring TransactionManager, you obviously need to read their doc on how that affect the session. The general principle applies though.

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


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.