-->
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.  [ 3 posts ] 
Author Message
 Post subject: update not executing?
PostPosted: Fri Oct 28, 2005 5:50 pm 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
i'm using Hibernate 3.0 with Tapestry 4.0 beta 11, using long sessions. i have a test application Wiki, and the edit page is updating my WikiPage object's properties correctly, and calling the DAO, which reports the correct changed values, but yet issues no SQL so the changed object is not persisted.

example:
1. retrieve WikiPage name:b, contents:b
2. update contents:bb > click Submit
3. log output results in:
[DEBUG] 3210277 wiki.components.WikiPageCRUD starting a transaction
[DEBUG] 3210277 wiki.dao.HibernateUtil Starting new database transaction in this thread.
[DEBUG] 3210277 wiki.dao.HibernateUtil Opening new Session for this thread.
[DEBUG] 3210277 wiki.dao.WikiPageDAOHibernate entered editPage
id: 2
name: b
contents: bb
created: 2005-10-27
modified: Fri Oct 28 17:42:00 EDT 2005
[DEBUG] 3210327 wiki.dao.HibernateUtil Committing database transaction of this thread.
[DEBUG] 3210327 wiki.dao.HibernateUtil Closing Session of this thread.

so... how can my object clearly be changed yet hibernate doesn't recognize it as changed?

thanks,
jeff



Hibernate version: 3.0

Mapping documents:
WikiPage.hbm.xml

Code:
<!DOCTYPE
   hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class
      name="wiki.model.WikiPage"
      table="pag_pages_tbl">
      <id
         name="id"
         column="pag_id">
         <generator class="native" />
      </id>
      <property
         name="name"
         type="string">
         <column
            name="pag_name"
            not-null="true" />
      </property>
      <property
         name="contents"
         type="string">
         <column
            name="pag_content"
            length="65535"
            not-null="true" />
      </property>
      <property name="dateCreated" type="date">
         <column name="pag_date_created" not-null="true"/>
      </property>
      <property name="dateModified" type="date">
         <column name="pag_date_modified" not-null="true"/>
      </property>

   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
using HibernateFilter from 3.0 open session in view from older version of hibernate's wiki:
Code:
public class HibernateFilter
      implements
      Filter {

   private static Log log = LogFactory.getLog(HibernateFilter.class);

   public void destroy() {}

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
         throws IOException,
         ServletException {
      try {
         // We don't start the database transaction here, but when first needed
         chain.doFilter(request, response);

         // Commit any pending database transaction.
         HibernateUtil.commitTransaction();
      }
      finally {
         // No matter what happens, close the Session.
         HibernateUtil.closeSession();
      }
   }

   public void init(FilterConfig filterConfig) throws ServletException {
      log.info("Servlet filter init, now opening/closing a Session for each request.");
   }

}


HibernateUtil from book or wiki:
Code:
public class HibernateUtil {

   private static Configuration configuration;

   private static Log log = LogFactory.getLog(HibernateUtil.class);
   private static SessionFactory sessionFactory;
   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();

   // Create the initial SessionFactory from the default configuration files
   static {
      try {
         configuration = new Configuration().configure();
         configuration.addResource("wiki/model/WikiPage.hbm.xml");
         sessionFactory = configuration.buildSessionFactory();
      }
      catch (Throwable ex) {
         // We have to catch Throwable, otherwise we will miss
         // NoClassDefFoundError and other subclasses of Error
         log.error("Building SessionFactory failed.", ex);
         throw new ExceptionInInitializerError(ex);
      }
   }

   /**
    * Start a new database transaction.
    */
   public static void beginTransaction() {
      // Would be written as a no-op in an EJB container with CMT
      Transaction tx = (Transaction) threadTransaction.get();
      if (tx == null) {
         log.debug("Starting new database transaction in this thread.");
         tx = getSession().beginTransaction();
         threadTransaction.set(tx);
      }
   }

   /**
    * Closes the Session local to the thread.
    */
   public static void closeSession() {
      // Would be written as a no-op in an EJB container with CMT
      Session s = (Session) threadSession.get();
      threadSession.set(null);
      if (s != null && s.isOpen()) {
         log.debug("Closing Session of this thread.");
         s.close();
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void commitTransaction() {
      // Would be written as a no-op in an EJB container with CMT
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
            log.debug("Committing database transaction of this thread.");
            tx.commit();
         }
         threadTransaction.set(null);
      }
      catch (HibernateException ex) {
         rollbackTransaction();
         throw ex;
      }
   }

   /**
    * Retrieves the current Session local to the thread. <p/> If no Session is
    * open, opens a new Session for the running thread.
    *
    * @return Session
    */
   public static Session getSession() {
      // With CMT, this should return getSessionFactory().getCurrentSession()
      // and do nothing else
      Session s = (Session) threadSession.get();
      if (s == null) {
         log.debug("Opening new Session for this thread.");
         s = getSessionFactory().openSession();
         threadSession.set(s);
      }
      return s;
   }

   /**
    * Returns the SessionFactory used for this static class.
    *
    * @return SessionFactory
    */
   public static SessionFactory getSessionFactory() {
      /*
       * Instead of a static variable, use JNDI: SessionFactory sessions =
       * null; try { Context ctx = new InitialContext(); String jndiName =
       * "java:hibernate/HibernateFactory"; sessions =
       * (SessionFactory)ctx.lookup(jndiName); } catch (NamingException ex) {
       * throw new RuntimeException(ex); } return sessions;
       */
      return sessionFactory;
   }

   /**
    * Rollback the database transaction.
    */
   public static void rollbackTransaction() {
      // Would be written as a no-op in an EJB container with CMT (maybe
      // setRollBackOnly...)
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         threadTransaction.set(null);
         if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
            log.debug("Tyring to rollback database transaction of this thread.");
            tx.rollback();
         }
      }
      finally {
         closeSession();
      }
   }

}



WikiPageDAO excerpt:
Code:
   public WikiPage editPage(WikiPage page) throws DAOException {
      log.debug("entered editPage "
            + "\nid: " + page.getId()
            + "\nname: " + page.getName()
            + "\ncontents: " + page.getContents()
            + "\ncreated: " + page.getDateCreated()
            + "\nmodified: " + page.getDateModified());
      try {
         HibernateUtil.getSession().lock(page, LockMode.NONE);
         HibernateUtil.getSession().update(page);
         return page;
      }
      catch (HibernateException e) {
         String msg = "The DAO could not update page.";
         log.error(msg, e);
         throw new DAOException(msg, e);
      }
   }


code in Tapestry page that uses the DAO after processing the Update form:
Code:
log.debug("starting a transaction");
HibernateUtil.beginTransaction();
wikiPage.setDateModified(new Date());
wdao.editPage(wikiPage);




Full stack trace of any exception that occurs: no errors...

Name and version of the database you are using: mySql 4.0.22

The generated SQL (show_sql=true): nothing generated...

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 11:38 am 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
turned on debugging for hibernate, here is the output when attempting to update the WikiPage object:

Code:
[DEBUG] 144618 org.hibernate.impl.SessionImpl opened session at timestamp: 4631644798930944
[DEBUG] 144618 org.hibernate.transaction.JDBCTransaction begin
[DEBUG] 144618 org.hibernate.jdbc.ConnectionManager opening JDBC connection
[DEBUG] 144618 org.hibernate.connection.DriverManagerConnectionProvider total checked-out connections: 0
[DEBUG] 144618 org.hibernate.connection.DriverManagerConnectionProvider using pooled JDBC connection, pool size: 0
[DEBUG] 144618 org.hibernate.transaction.JDBCTransaction current autocommit status: false
[DEBUG] 144628 wiki.dao.WikiPageDAOHibernate entered editPage
id: 2
name: b
contents: bB
created: 2005-10-27
modified: Mon Oct 31 10:30:55 EST 2005
[DEBUG] 144628 org.hibernate.engine.Cascades id unsaved-value: null
[DEBUG] 144638 org.hibernate.event.def.AbstractReassociateEventListener reassociating transient instance: [wiki.model.WikiPage#2]
[DEBUG] 144658 org.hibernate.event.def.DefaultSaveOrUpdateEventListener ignoring persistent instance
[DEBUG] 144658 org.hibernate.event.def.DefaultSaveOrUpdateEventListener object already associated with session: [wiki.model.WikiPage#2]
[DEBUG] 144678 wiki.dao.HibernateUtil Committing database transaction of this thread.
[DEBUG] 144678 org.hibernate.transaction.JDBCTransaction commit
[DEBUG] 144678 org.hibernate.impl.SessionImpl automatically flushing session
[DEBUG] 144678 org.hibernate.event.def.AbstractFlushingEventListener flushing session
[DEBUG] 144678 org.hibernate.event.def.AbstractFlushingEventListener processing flush-time cascades
[DEBUG] 144688 org.hibernate.event.def.AbstractFlushingEventListener dirty checking collections
[DEBUG] 144688 org.hibernate.event.def.AbstractFlushingEventListener Flushing entities and processing referenced collections
[DEBUG] 144768 org.hibernate.event.def.AbstractFlushingEventListener Processing unreferenced collections
[DEBUG] 144778 org.hibernate.event.def.AbstractFlushingEventListener Scheduling collection removes/(re)creates/updates
[DEBUG] 144778 org.hibernate.event.def.AbstractFlushingEventListener Flushed: 0 insertions, 0 updates, 0 deletionsto 1 objects
[DEBUG] 144778 org.hibernate.event.def.AbstractFlushingEventListener Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
[DEBUG] 144778 org.hibernate.pretty.Printer listing entities:
[DEBUG] 144778 org.hibernate.pretty.Printer wiki.model.WikiPage{contents=bB, dateModified=31 October 2005, dateCreated=27 October 2005, name=b, id=2}
[DEBUG] 144778 org.hibernate.event.def.AbstractFlushingEventListener executing flush
[DEBUG] 144778 org.hibernate.event.def.AbstractFlushingEventListener post flush
[DEBUG] 144778 org.hibernate.jdbc.JDBCContext before transaction completion
[DEBUG] 144778 org.hibernate.impl.SessionImpl before transaction completion
[DEBUG] 144778 org.hibernate.transaction.JDBCTransaction committed JDBC Connection
[DEBUG] 144778 org.hibernate.jdbc.JDBCContext after transaction completion
[DEBUG] 144788 org.hibernate.impl.SessionImpl after transaction completion
[DEBUG] 144788 wiki.dao.HibernateUtil Closing Session of this thread.
[DEBUG] 144788 org.hibernate.impl.SessionImpl closing session
[DEBUG] 144788 org.hibernate.jdbc.ConnectionManager closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[DEBUG] 144788 org.hibernate.connection.DriverManagerConnectionProvider returning connection to pool, pool size: 1
[DEBUG] 144788 org.hibernate.jdbc.JDBCContext after transaction completion
[DEBUG] 144788 org.hibernate.impl.SessionImpl after transaction completion



as you can see, even the hibernate pretty printer reports the new updated value
[DEBUG] 144778 org.hibernate.pretty.Printer wiki.model.WikiPage{contents=bB, dateModified=31 October 2005, dateCreated=27 October 2005, name=b, id=2}

so why isn't any sql being issued?


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Mon Oct 31, 2005 1:06 pm 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
appears to be a Stupid User Error:

i was calling lock() immediately prior to update()... hence the changes to the object prior to lock() are ignored.

removing lock() and just calling update() works as expected.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.