-->
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: HibernateContext not persisting data???
PostPosted: Wed Mar 02, 2005 10:44 pm 
Newbie

Joined: Thu Jan 20, 2005 3:02 pm
Posts: 13
b]Hibernate version: [/b] 2.1.8

Name and version of the database you are using: MySQL 4

JBoss version: 4.0.1sp1

Hello,

I've switched over from using the HibernateUtil to the new HibernateContext to obtain and use Hibernate Sessions.

I have the following usage scenerio:

Web (ServletContextListener) --> Command (EJB) --> DAO (Contains Hibernate Code (HibernateContext))

Problem

I can retrieve Hibernate Objects using the HibernateContext Utility but I cannot save (persist any). When I switch back to using the HibernateUtil method (as outlined in the Hibernate in Action book) it works, and all is persisted! Following is my configuration and code:


Config files:

hibernate-service.xml (deployed as a .har file)

Code:
<server>
   <mbean code="org.jboss.hibernate.jmx.Hibernate"
      name="jboss.har:service=Hibernate">
      <attribute name="DatasourceName">
         java:/SwiftyDS
      </attribute>
      <attribute name="Dialect">
         net.sf.hibernate.dialect.MySQLDialect
      </attribute>
      <attribute name="SessionFactoryName">
         java:/hibernate/SessionFactory
      </attribute>
      <attribute name="CacheProviderClass">
         net.sf.hibernate.cache.HashtableCacheProvider
      </attribute>
   </mbean>
</server>


DAO files:

Code:
public void updateStates(List states)
    {
       
               
        Session session = HibernateContext.getSession(IConstants.HIBERNATE_SESSION_FACOTRY);
                     
        List databaseStates = this.getAllStates();   
        for(Iterator it = states.iterator(); it.hasNext(); )
        {
          State state = (State) it.next();
          try
          {
            if(!databaseStates.contains(state))
                  session.saveOrUpdate(state);
          }
          catch (HibernateException hex)
          {
              throw new InfrastructureException(this.getClass(), "Trouble while executing updateStates.", hex);
          }
        }
       
    }
   
       
    /**
     * Gets all states
     *
     * @return a list of all states
     */
    public List getAllStates()
    {
        Session session = HibernateContext.getSession(IConstants.HIBERNATE_SESSION_FACOTRY);
        List states = new ArrayList();
       
        try
        {
            states = session.createCriteria(State.class).list();
        }
        catch(HibernateException hex)
        {
            throw new InfrastructureException(this.getClass(), "Trouble while trying to acquire all states", hex);
        }
       
        return states;
    }


Now, I can call the getAllStates method and it works fine, however, the State objects are not getting persisted to the database.

Funny thing is though when I make a call (from the web listener) directly with NO EJB involvement, with the following code, everything works fine (all data is entered into the DB):


Code:
        try
        {
            HibernateUtil.beginTransaction();
            StateDAO stateDAO = new StateDAO();
       
            //UPDATE STATES
            stateDAO.updateStates(digester.getStates());
           
            //COMMIT (SAVE)
            HibernateUtil.commitTransaction();
        }
        finally
        {
            HibernateUtil.closeSession();
        }


Would anyone have a idea as to why the HibernateContext is not saving the objects' state?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 05, 2005 10:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I believe that version may not be doing the auto-flushing during txn commit. Try the later releases of JB4.


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.