-->
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: Query after exception in transaction?
PostPosted: Thu May 10, 2007 5:44 am 
Newbie

Joined: Wed Sep 20, 2006 6:33 am
Posts: 5
Hibernate version: 3.1

Mapping documents:
Code:
<class name="ru.myapp.model.Tag" table="tag">
  <id name="id" column="id" type="long">
    <generator class="identity"/>
  </id>
 
  <property name="name" column="name"/>
</class>


Code between sessionFactory.openSession() and session.close():
Code:
Session s = sf.openSession();
Transaction tx = null;
try
{
   tx = s.beginTransaction();

   Tag t = new Tag();
   t.setName("a");
   s.save(t);

   tx.commit();
}
catch (HibernateException e)
{
   if (tx != null)
   {
      Tag t1 = (Tag) s.createQuery("from Tag where name=:name").setString("name", "a").list().get(0);
      System.out.println("loaded tag id: " + t1.getId());
      System.out.println("loaded tag name: " + t1.getName());

      tx.rollback();
   }
}
finally
{
   s.close();
}


Full stack trace of any exception that occurs:
Code:
org.hibernate.AssertionFailure: null id in ru.myapp.model.Tag entry (don't flush the Session after an exception occurs)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:48)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:150)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:106)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
   at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
   at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:951)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:109)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:88)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1540)
   at test.HSessionFactoryTest.main(HSessionFactoryTest.java:78)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
Exception in thread "main" org.hibernate.AssertionFailure: null id in ru.myapp.model.Tag entry (don't flush the Session after an exception occurs)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:48)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:150)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:106)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
   at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
   at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:951)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:109)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:88)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1540)
   at test.HSessionFactoryTest.main(HSessionFactoryTest.java:78)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)


Name and version of the database you are using:
mysql version 5.0.24, tabel type is MyISAM

Hello.

I have the following situation. The table Tag holds tags as id and name. Name of a tag is UNIQUE. I want, when a new tag is added and it is already exists, to load an existing tag, but I get the following exception as shown above. How to make a query after an exception occurs during a transaction. In the example above the exception occurs due to the UNIQUE constraint on the name field.


Thank you.

_________________
Grigory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 5:50 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can not ever use a Session again after it has thrown an exception. Read the documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 6:00 am 
Newbie

Joined: Wed Sep 20, 2006 6:33 am
Posts: 5
But what if I get this exception in OpenSessionInViewFilter?

The whole picture is the following:
A user adds tags to entities. During a request my webapp creates a tag and if it exists it adds an existing tag to an entity? But when during creation an exception occurs then I cannot load an existing tag and add it to an entity?

Could you, please, explain?

_________________
Grigory.


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.