0

Hibernate + ZK: createQuery is not valid without active transaction

asked 2008-10-28 09:39:36 +0800

deuter gravatar image deuter
15 1 1 1

Hi,

I get this error message in 2 different ZK events, each running an own java thread, having a session and having non equal transactions.

I use HibernateSessionContextListener, as proposed in Small Talks - Hibernate + ZK.

What am I doing wrong? Or what have i forgotten?

in hibernate cfg file I use...
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>

best regards,
martin

delete flag offensive retag edit

12 Replies

Sort by » oldest newest

answered 2008-10-28 11:15:33 +0800

robbiecheng gravatar image robbiecheng
1144 2
http://robbiecheng.sys-co...

Hi martin,

You have to open a transaction before executing any sql query.
Or you can add opensessioninview listener in your zk.xml

	<!-- Hibernate OpenSessionInView Pattern -->
	<listener>
		<description>Hibernate Open Session In View life-cycle</description>
		<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
	</listener>

/robbie

link publish delete flag offensive edit

answered 2008-10-28 12:02:22 +0800

deuter gravatar image deuter
15 1 1 1

Hi Robbie,

thats exactly what I'm doing and this is the confusing point...

First I do...
// Start Hibernate
SessionFactory sf = HibernateUtil.getSessionFactory(connectionInfo.getXDB_ConnectionInfo());
Session session = sf.getCurrentSession();
Transaction ta = session.beginTransaction();

Then I do a lot of

link publish delete flag offensive edit

answered 2008-10-28 12:08:05 +0800

deuter gravatar image deuter
15 1 1 1

sorry, click error..

After beginning a transaction...

I do a lot of
findAll, findById, do some processing of the database objects and write these to an XML structure for output...

and at last I do
ta.commit();

Now, if I call this sequence in ZK twice, both have an active transaction at first. But then I get this Hibernate exception, that the first started transaction is not active :-(

I don't know, how this transaction vanishes, I don't get any log message about this.

What am I doing wrong? Or is there something not possible with Hibernate built in TransactionFactory ...
Do I need a JTA Manager together with Apache Tomcat...

Lots of questions, few answers ...

best regards,
martin

link publish delete flag offensive edit

answered 2008-10-29 11:01:45 +0800

robbiecheng gravatar image robbiecheng
1144 2
http://robbiecheng.sys-co...

Hi martin,

simply add the following configurations into your zk.xml

<!-- Hibernate SessionFactory lifecycle -->
	<listener>
		<description>Hibernate SessionFactory lifecycle</description>
		<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
	</listener>
	
	<!-- Hibernate OpenSessionInView Pattern -->
	<listener>
		<description>Hibernate Open Session In View life-cycle</description>
		<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
	</listener>

	<!-- Hibernate thread session context handler -->
	<listener>
		<description>Hibernate thread session context handler</description>
		<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionContextListener</listener-class>
	</listener>  

And you no longer worry about the problem of transaction, simply invoke org.zkoss.zkplus.hibernate.HibernateUtil to get current session, and do whatever you want as follows:

   Session currentSession() {
    	return org.zkoss.zkplus.hibernate.HibernateUtil.currentSession();
    }
    
public List findAll() {
    	Session sess =  currentSession();    	
      
      return sess.createQuery("from Event").list();
    }

/robbie

link publish delete flag offensive edit

answered 2009-03-04 19:59:59 +0800

bob007 gravatar image bob007
384 3 4

updated 2009-03-04 20:18:30 +0800

Hi all,

I have a similar problem. I did like in Small Talks - Hibernate + ZK exemple. And it worked well until i add a many-to-one relation in my hibernate mapping. Now i can load my objects with the referenced objects and access it. BUT it seem I can't save a new object (with many-to-one relation) without create a transaction and i don't think it's a good thing to do in zk.

If I do like in the smalltalk exemple: I don’t create a transaction i can see the insert into (…) line in the console BUT its never added to the database.

Why this behavior?
Should I create a transaction then ? When ? Each time ?

My mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="heuresEffectuees.HeuresEffectuees" table="heuresEffectuees">
      <composite-id>
         <key-property name="noBon"/>
         <key-property name="cipRepondant"/>
      </composite-id>
      <property name="nbHeuresEffectuees"/>
      <many-to-one name="repondant" column="cipRepondant" class="repondants.Repondant" lazy="false" insert="false" update="false"/>
   </class>
</hibernate-mapping>


My DAO
public class HeuresEffectueesDAO {
   Session currentSession() {
      return HibernateUtil.currentSession();
   }

   //!!!!!!!!     DON’T WORK   !!!!!!!!
   public void saveOrUpdate(HeuresEffectuees desHeuresEffectuees) {
      Session sess = currentSession();
      sess.saveOrUpdate(desHeuresEffectuees);      
   }

   //!!!!!!!!!!!!    WORK    !!!!!!!!!!!
   public void saveOrUpdate2(HeuresEffectuees desHeuresEffectuees) {
      Session sess = currentSession();
      Transaction tx = sess.beginTransaction();
      sess.saveOrUpdate(desHeuresEffectuees);
      tx.commit();
   }

   public void delete(HeuresEffectuees desHeuresEffectuees) {
      Session sess = currentSession();
      sess.delete(desHeuresEffectuees);
   }
   
   public List findAllByNoBon(String noBon) {
      Session sess = currentSession();      
      Criteria criteria = sess.createCriteria(HeuresEffectuees.class);
      criteria.add(Restrictions.like("noBon", noBon));      
      return criteria.list();      
   }

   public HeuresEffectuees findByNoBonEtRepondant(String noBon, String cipRepondant) {
      Session sess = currentSession();
      return (HeuresEffectuees) sess.get(HeuresEffectuees.class, cipRepondant);
   }

   public List findAll() {
      Session sess = currentSession(); 
      return sess.createQuery("from HeuresEffectuees").list();
   }
}

link publish delete flag offensive edit

answered 2009-03-09 09:38:49 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

ZK does not assume anything. If you apply "open session in view" pattern, then you don't have to do the "transaction" yourself. Otherwise, you have to open a transaction and commit yourself.

link publish delete flag offensive edit

answered 2010-02-13 11:38:55 +0800

Aqquoz gravatar image Aqquoz
3

Hi robbie cheng & henrichen: I've exactly same problem with bob007. but i'm using netbeans 6.8 IDE . where should I put zk.xml file. thx a lot. I'm totally newbie to JavaEE things...

link publish delete flag offensive edit

answered 2010-02-18 22:01:10 +0800

samchuang gravatar image samchuang
4084 4

Hi

I use eclipse, and the zk.xml is in the WEB-INF folder, i think the foler is the same.

link publish delete flag offensive edit

answered 2011-10-27 08:05:08 +0800

RichardL gravatar image RichardL
768 4

updated 2011-10-27 08:19:05 +0800

@henrichen In this case, the documentation at http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Integration/Hibernate should be revised to reflect this, either by specifying that the "open session in view" pattern is used in the example, or add the Hibernate beginTransaction and commit method calls to the methods of the EventDAO class.

link publish delete flag offensive edit

answered 2011-10-28 05:34:33 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

Hi, RichardL,

Have edit a bit. Hope that will clear some confusion. Thanks.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-10-28 09:39:36 +0800

Seen: 6,763 times

Last updated: Mar 15 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More