-->
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.  [ 6 posts ] 
Author Message
 Post subject: Update or Delete not working
PostPosted: Thu May 12, 2005 11:05 am 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Ok now I am completely lost. Please someone help me out. I can't figure this out. I migrated from Hibernate 2 to Hibernate 3. I am trying to do a simple update and it is not working. I can save a new data, but I can not update or delete. I was using Hibernate Synchronizer but after fighting with it for two days and posting for help everywhere I got no where. So for simplicity I now tried using HibernateUtil and even then it does not work. The following is Types.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping default-lazy="false"  package="com.at.hib.persistence">
   <class name="Types" table="types">
   <cache usage="transactional"/>
      <id
         column="Types_ID"
         name="Id"
         type="integer"
      >
         <generator class="native" />
      </id>
      <property
         column="Type"
         length="30"
         name="Type"
         not-null="false"
         type="string"
       />
      <property
         column="description"
         name="Description"
         not-null="false"
         type="string"
       />
      <set
         inverse="true"
         lazy="true"
         name="ProductsSet"
      >
         <key column="Types_ID" />
         <one-to-many class="Products" />
      </set>
   </class>
</hibernate-mapping>


The following is HibernateUtil

Code:
public class HibernateUtil {
   private static Log log = LogFactory.getLog(HibernateUtil.class);

   private static Configuration configuration;
   private static SessionFactory sessionFactory;
   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();
   private static final ThreadLocal threadInterceptor = new ThreadLocal();

   /**
    * 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 InfrastructureException(ex);
      }
      return sessions;
   }

   /**
    * Returns the original Hibernate configuration.
    *
    * @return Configuration
    */
   public static Configuration getConfiguration() {
      return configuration;
   }

   /**
    * Rebuild the SessionFactory with the static Configuration.
    *
    */
    public static void rebuildSessionFactory()
      throws InfrastructureException {
      synchronized(sessionFactory) {
         try {
            sessionFactory = getConfiguration().buildSessionFactory();
         } catch (Exception ex) {
            throw new InfrastructureException(ex);
         }
      }
    }

   /**
    * Rebuild the SessionFactory with the given Hibernate Configuration.
    *
    * @param cfg
    */
    public static void rebuildSessionFactory(Configuration cfg)
      throws InfrastructureException {
      synchronized(sessionFactory) {
         try {
            sessionFactory = cfg.buildSessionFactory();
            configuration = cfg;
         } catch (Exception ex) {
            throw new InfrastructureException(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()
      throws InfrastructureException {
      Session s = (Session) threadSession.get();
      try {
         if (s == null) {
            log.debug("Opening new Session for this thread.");
            if (getInterceptor() != null) {
               log.debug("Using interceptor: " + getInterceptor().getClass());
               s = getSessionFactory().openSession(getInterceptor());
            } else {
               s = getSessionFactory().openSession();
            }
            threadSession.set(s);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return s;
   }

   /**
    * Closes the Session local to the thread.
    */
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
         threadSession.set(null);
         if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread.");
            s.close();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Start a new database transaction.
    */
   public static void beginTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx == null) {
            log.debug("Starting new database transaction in this thread.");
            tx = getSession().beginTransaction();
            threadTransaction.set(tx);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void commitTransaction()
      throws InfrastructureException {
      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 new InfrastructureException(ex);
      }
   }

   /**
    * Commit the database transaction.
    */
   public static void rollbackTransaction()
      throws InfrastructureException {
      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();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      } finally {
         closeSession();
      }
   }

   /**
    * Reconnects a Hibernate Session to the current Thread.
    *
    * @param session The Hibernate Session to be reconnected.
    */
   public static void reconnect(Session session)
      throws InfrastructureException {
      try {
         session.reconnect();
         threadSession.set(session);
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

   /**
    * Disconnect and return Session from current Thread.
    *
    * @return Session the disconnected Session
    */
   public static Session disconnectSession()
      throws InfrastructureException {

      Session session = getSession();
      try {
         threadSession.set(null);
         if (session.isConnected() && session.isOpen())
            session.disconnect();
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return session;
   }

   /**
    * Register a Hibernate interceptor with the current thread.
    * <p>
    * Every Session opened is opened with this interceptor after
    * registration. Has no effect if the current Session of the
    * thread is already open, effective on next close()/getSession().
    */
   public static void registerInterceptor(Interceptor interceptor) {
      threadInterceptor.set(interceptor);
   }

   private static Interceptor getInterceptor() {
      Interceptor interceptor =
         (Interceptor) threadInterceptor.get();
      return interceptor;
   }
}

The following is a simple function that makes the update using HibernateUtil inside of my EJB:

Code:
@MethodPermissions({"Sales"})
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void newsimpleupdate()     {
            try {
         Session session = HibernateUtil.getSession();
              Transaction tx = session.beginTransaction();
             
              Types mytype = new Types(17);
              mytype.setId(17);
              mytype.setDescription("my new test");
              mytype.setType("Test5");
             
              session.update(mytype);
             
              tx.commit();
              HibernateUtil.closeSession();
            
            System.out.println("Updated Type to " + mytype.getType());
            }
            catch (Exception e) {log.error(e.getMessage()); }
   }


I am using JBoss and I am packaging Hibernate stuff inside a HAR file.
The following is my hibernate-service.xml

Code:
<server>
- <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
  <attribute name="DatasourceName">java:/MySqlDS</attribute>
  <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute>
  <attribute name="SessionFactoryName">java:/hibernate/HibernateFactory</attribute>
  <attribute name="CacheProviderClass">org.hibernate.cache.TreeCacheProvider</attribute>
  <attribute name="ShowSqlEnabled">false</attribute>
  </mbean>
  </server>


I am not getting any error messages and the update is not happening at the Database layer. Does anybody see what might be the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 2:06 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Use show_sql = 'true' to see if sql statements are executed.
Try debuging Transaction object to see if it is commited.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:57 am 
Newbie

Joined: Thu Mar 17, 2005 11:13 am
Posts: 7
Are you using JBoss 4.0.2?

There's some discussion about similar problem in JBoss forums:

http://www.jboss.org/index.html?module= ... ic&t=63884

Not a solution found yet, AFAIK.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 11:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Please simplify your scenario to the simpliest form reproducing the issue and create a JIRA case, attaching the test case.

I'll take a look.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 10:31 am 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
I think I have figured out the problem but need some help, PLEASE. It seems that whenever I am using the SessionFactory that is retreived from JNDI I cannot update or delete (I know it sounds wierd). However when I create a new SessionFactory without retrieving from JNDI everything works fine and no problem. The only reason we do not want to create a SessionFactory on our own is that we wanted the Application Server (JBoss) to be reponsible for managing the session and what not. Has anyone faced anything like this?

We are using JBoss 4.0.2, Hibernate 3.0.3, EJB 3 Preview 3.

Please someone tell me there is something I can do. I am going crazy because it makes no sense why I can save but not update or delete to the database


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 4:38 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
I think there is a serious problem!!! Whom do I notify of this problem and how.
Server Configuration:

JBoss 4.0.2
EJB 3 Preview 5
Hibernate 3.0.3
Hibernate mappings, code, DAO all mapped in a HAR file

Data saved, updated, deleted at the EJB layer.

Example
Code:
@MethodPermissions({"Sales"})
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void newsimpleupdate()     {
            try {
              Session session = HibernateUtil.getSession();
              Transaction tx = session.beginTransaction();
             
              Types mytype = new Types(17);
              mytype.setId(17);
              mytype.setDescription("my new test");
              mytype.setType("Test5");
             
              session.update(mytype);
             
              tx.commit();
              HibernateUtil.closeSession();
           
            System.out.println("Updated Type to " + mytype.getType());
            }
            catch (Exception e) {log.error(e.getMessage()); }
   }


Using Session Factory from JBoss results in commit failure in methods of update and delete. Save works fine!


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