-->
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.  [ 1 post ] 
Author Message
 Post subject: persist is not valid without active transaction Error
PostPosted: Tue Aug 04, 2009 1:15 am 
Newbie

Joined: Tue Aug 04, 2009 1:02 am
Posts: 2
Hello all,

I am trying to do a simple persist operation using hibernate and Mysql but getting the error 'org.hibernate.HibernateException: persist is not valid without active transaction'. I have the persist operation enclosed within a transaction.

This is the test class:
Code:
public class HibernateTester {

   public static void main(String[] args) {
   ResourceHome resourceHome = new ResourceHome();
   Resource rs1 = new Resource();
   Transaction tx = resourceHome.getSessionFactory().getCurrentSession().beginTransaction();
   rs1.setId(3);
   rs1.setName("san");
   rs1.setLocation("basement");
   rs1.setImage("");
   resourceHome.persist(rs1);
   tx.commit();

   }
}



The config file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.show.sql">true</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.password">san@12345</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/rm</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="current_session_context_class">thread</property>
      <mapping resource="com/san/rm/hibernate/Resource.hbm.xml"></mapping>
      <mapping resource="com/san/rm/hibernate/Booking.hbm.xml"></mapping>
      
   </session-factory>
</hibernate-configuration>



The HBM file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 2, 2009 6:50:37 PM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="com.san.rm.hibernate.Resource" table="resource" >
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" not-null="true" />
        </property>
        <property name="location" type="string">
            <column name="location" length="45" not-null="true" />
        </property>
        <property name="image" type="string">
            <column name="image" length="45" not-null="true" />
        </property>

       
    </class>
</hibernate-mapping>


The DAO class created by hibernate tools:
Code:
public class ResourceHome {

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

   private final SessionFactory sessionFactory = getSessionFactory();

   public SessionFactory getSessionFactory() {
      try {

         //SessionFactory sessionFactory = (SessionFactory) new InitialContext()
         //      .lookup("SessionFactory");


         SessionFactory sessionFactory = null;
         if(sessionFactory==null)
         {
            sessionFactory = new Configuration().configure().buildSessionFactory();
         }
         return sessionFactory;
      } catch (Exception e) {
         e.printStackTrace();
         throw new IllegalStateException(
               "Could not locate SessionFactory in JNDI");
      }
   }

   public void persist(Resource transientInstance) {
      log.debug("persisting Resource instance");
      try {
         Session ses = sessionFactory.getCurrentSession();
         ses.persist(transientInstance);
         log.debug("persist successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
   }


}




Now in the above DAO , if I change the call getCurrentSession() to openSession(), everything works fine without any error
but nothing gets persisted to the database.

A manual creation of session and doing a save without using the DAO is working.

Please help me here.

Thanks!


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

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.