-->
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.  [ 7 posts ] 
Author Message
 Post subject: persist is not valid without active transaction
PostPosted: Tue Jun 05, 2007 12:33 am 
Newbie

Joined: Mon Oct 17, 2005 3:26 pm
Posts: 4
Hibernate version:
3.2

I used hbm2dao to generate my DAO PersonHome.java, and I assume the following method is to create/persist a new Person into the database:

Code:
    public void persist(Person transientInstance) {
        log.debug("persisting Person instance");
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        }
        catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }


However, I get the following exception:

Code:
org.hibernate.HibernateException: persist is not valid without active transaction
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
   at $Proxy0.persist(Unknown Source)
   at org.vinotrails.model.dao.PersonHome.persist(PersonHome.java:39)...


I understand (from the example) that you should call beginTransaction() on the session before calling save(). So how is the persist() method supposed to function in this generated DAO?

Furthermore, is there documentation on what the generated methods in this PersonHome class are supposed to do exactly, such as attachDirty, attachClean, merge?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use a layer above the DAOs to control the Transaction boundaries. This might be a CMT method or a manually managed transaction boundary. This allows the DAO methods to be called when you please under a what ever transaction is required for the current execution context.


Top
 Profile  
 
 Post subject: Re: persist is not valid without active transaction
PostPosted: Sun Jul 08, 2007 3:48 pm 
Newbie

Joined: Sun Oct 22, 2006 9:28 am
Posts: 13
dwichman wrote:
...Furthermore, is there documentation on what the generated methods in this PersonHome class are supposed to do exactly, such as attachDirty, attachClean, merge?...


I'm looking for that kind of documentation, too.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 9:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Not really but the methods are short and use the hibernate core API which is well documented. Just as the persist DAO method is really a wrapper to Hibernate's session.persist method thus API docs for hibernate has all the information you require.


Top
 Profile  
 
 Post subject: I have the same problem...
PostPosted: Wed Jul 11, 2007 9:07 pm 
Newbie

Joined: Wed Jul 11, 2007 8:51 pm
Posts: 2
Location: Sydney, Australia
I am writing a test on a dao using hsqldb:

public class DataDaoTestCase {

private SessionFactory mSessionFactory;
private Session mSession;
private Transaction mTransaction;
private DataDao mDataDao;

protected void setUp() throws Exception {
mSessionFactory = createSessionFactory();
mDataDao = new HibernateDataDao(mSessionFactory);
mSession = mSessionFactory.openSession();
mTransaction = mSession.beginTransaction();
}

public void testPersist() {
...
mDataDao.save(aDataObject);
...
}
}

My HibernateDataDao is:
public class HibernateDataDao implements DataDao {
private SessionFactory mSessionFactory;
public HibernateDataDao(SessionFactory aSessionFactory) {
mSessionFactory = aSessionFactory;
}
public Long save(DataObject aDataObject) {
return (Long) mSessionFactory
.getCurrentSession().save(aDataObject);
}
}

But I still get this exception:
org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy8.save(Unknown Source)
at HibernateDataDao.save(HibernateDataDao.java:93)
at HibernateDataDaoTestCase.testSave(HibernateDataDaoTestCase.java:122)

My hibernate.cfg.xml is:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:aname</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>

<property name="hibernate.hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>

Mapping resources are added in createSessionFactory() method.


Top
 Profile  
 
 Post subject: My case is resolved.
PostPosted: Wed Jul 11, 2007 9:41 pm 
Newbie

Joined: Wed Jul 11, 2007 8:51 pm
Posts: 2
Location: Sydney, Australia
In the setUp() of the test class, instead of using:
mSession = mSessionFactory.openSession();
mTransaction = mSession.beginTransaction();

use:
mSession = mSessionFactory.getCurrentSession();
mTransaction = mSession.beginTransaction();

My understanding is that:
getCurrentSession() will create a new session if there is none bound to the thread context, while openSession() does not bound the new session to the thread context.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 9:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You are correct getCurrentSession() is the recommended approach as it helps you control the tx/session context for free.


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