Hello,
I need urgently some advice, please help. I have an unique constraint defined on column Directorynumber in table Keydirectory. The app uses a dialog to create new Keydirectories. When I try to insert a new Keydirectory with an already existing Directorynumber (upon pressing the accept or the ok button), I get the first exception below. That is ok. I try to evict the object from the session in order to let the user correct the data in the dialog. Although the Directorynumber is now unique, I get the second exception below (upon pressing the Accept or Ok button).
While debugging I found the reason: The insert is still in the "insertions"-ArrayList of the session. Is this an error in the evict() method? Do I have to do more than evict()? Or do I really have to throw away the session? This is really not preferred by me since all other changes of the session would be lost as well.
Note: a transaction covers the code below.
Please help me asap.
Thanks in advance,
Georgios Skempes
Hibernate version:
2.1.7.c
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.tsystems.mighty.persistence">
<class name="Keydirectory" table="KEYDIRECTORY">
<id
column="ID"
name="Id"
type="integer"
>
<generator class="vm" />
</id>
<property
column="DIRECTORYNUMBER"
length="10"
name="Directorynumber"
not-null="true"
type="integer"
/>
..........
<set
inverse="true"
lazy="true"
name="KEYSet"
>
<key column="DIRECTORYID" />
<one-to-many class="Key" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Integer oldId = myKeyDir.getId();
try {
writeSession.saveOrUpdate(myKeyDir);
writeSession.flush();
myKeyDir.setDirty(false);
} catch (HibernateException e) {
try {
// Remove the Keydirectory from the session and reset it to insert mode
// (for the next try to save)
writeSession.evict(myKeyDir);
if (oldId == null) {
myKeyDir.setId(null);
}
} catch (HibernateException e1) {
logger.error("Evicting a Keydirectory from the session failed", e);
throw new DatabaseException(messages.getMessage(1210, myKeyDir), e);
}
if (e.getCause() instanceof SQLException) {
int errorCode = ((SQLException) e.getCause()).getErrorCode();
if (errorCode == ISCConstants.isc_unique_key_violation) {
// Duplicate Value at unique key
logger.error("Couldn't INSERT or UPDATE the Keydirectory " + myKeyDir
+ " in database - check of unique constraint failed:", e);
throw new DatabaseException(messages.getMessage(1212, myKeyDir));
}
} else {
logger.error("Couldn't INSERT or UPDATE the Keydirectory " + myKeyDir + " INTO the DB.", e);
throw new DatabaseException(messages.getMessage(1209, myKeyDir), e);
}
}
return retval;
Full stack trace of any exception that occurs:
1) First try to insert a new Keydirectory with non-unique Directorynumber:
Hibernate: insert into KEYDIRECTORY (ISACTIVE, DIRECTORYNUMBER, COMMENT, TOPIC, STATEID, ID) values (?, ?, ?, ?, ?, ?)
09:00:49,780 ERROR JDBCExceptionReporter:58 - GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "UNQ1_KEYDIRECTORY" on table "KEYDIRECTORY"
09:00:49,795 ERROR SessionImpl:2399 - Could not synchronize database state with session
09:00:49,795 ERROR KeyDataAccess:336 - Couldn't INSERT or UPDATE the Keydirectory 15 in database - check of unique constraint failed:
net.sf.hibernate.exception.GenericJDBCException: could not insert: [com.tsystems.mighty.persistence.Keydirectory#16]
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:81)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:474)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:438)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2391)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at com.tsystems.mighty.dataaccess.KeyDataAccess.saveOrUpdateKeyDirectory(KeyDataAccess.java:317)
at com.tsystems.mighty.key.KeyModel.saveKeyDirectories(KeyModel.java:251)
at com.tsystems.mighty.key.KeyDirectoryView.actionPerformed(KeyDirectoryView.java:329)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)
at java.awt.Dialog$1.run(Dialog.java:515)
at java.awt.Dialog.show(Dialog.java:536)
at java.awt.Component.show(Component.java:1300)
at java.awt.Component.setVisible(Component.java:1253)
at com.tsystems.mighty.key.KeyView.showKeyDirectory(KeyView.java:599)
at com.tsystems.mighty.key.KeyView.actionPerformed(KeyView.java:583)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "UNQ1_KEYDIRECTORY" on table "KEYDIRECTORY"
at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:503)
at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:144)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeUpdate(FilterPreparedStatement.java:71)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:464)
... 60 more
1) Second try to insert the modified Keydirectory with unique Directorynumber:
Hibernate: insert into KEYDIRECTORY (ISACTIVE, DIRECTORYNUMBER, COMMENT, TOPIC, STATEID, ID) values (?, ?, ?, ?, ?, ?)
09:05:19,076 ERROR JDBCExceptionReporter:58 - GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "UNQ1_KEYDIRECTORY" on table "KEYDIRECTORY"
09:05:19,076 ERROR SessionImpl:2399 - Could not synchronize database state with session
09:05:19,076 ERROR KeyDataAccess:336 - Couldn't INSERT or UPDATE the Keydirectory 16 in database - check of unique constraint failed:
net.sf.hibernate.exception.GenericJDBCException: could not insert: [com.tsystems.mighty.persistence.Keydirectory#16]
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:81)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:474)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:438)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2391)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at com.tsystems.mighty.dataaccess.KeyDataAccess.saveOrUpdateKeyDirectory(KeyDataAccess.java:317)
at com.tsystems.mighty.key.KeyModel.saveKeyDirectories(KeyModel.java:251)
at com.tsystems.mighty.key.KeyDirectoryView.actionPerformed(KeyDirectoryView.java:329)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)
at java.awt.Dialog$1.run(Dialog.java:515)
at java.awt.Dialog.show(Dialog.java:536)
at java.awt.Component.show(Component.java:1300)
at java.awt.Component.setVisible(Component.java:1253)
at com.tsystems.mighty.key.KeyView.showKeyDirectory(KeyView.java:599)
at com.tsystems.mighty.key.KeyView.actionPerformed(KeyView.java:583)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "UNQ1_KEYDIRECTORY" on table "KEYDIRECTORY"
at org.firebirdsql.jdbc.AbstractPreparedStatement.internalExecute(AbstractPreparedStatement.java:503)
at org.firebirdsql.jdbc.AbstractPreparedStatement.executeUpdate(AbstractPreparedStatement.java:144)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeUpdate(FilterPreparedStatement.java:71)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:464)
... 60 more
Name and version of the database you are using:
Firebird 1.5.2
The generated SQL (show_sql=true):
see above
Debug level Hibernate log excerpt:
_________________ Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25
|