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.