Hibernate version:
3.0.5
Mapping documents:
OLIPort
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="gr.forthnet.enosis.oli.pojos.OLIPort"
table="ports"
>
<id
name="id"
column="id"
type="java.lang.Integer"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-OLIPort.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="code"
/>
<bag
name="localizedViews"
lazy="false"
inverse="true"
cascade="all"
>
<key
column="port"
>
</key>
<one-to-many
class="gr.forthnet.enosis.oli.pojos.PortLocalization"
/>
</bag>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-OLIPort.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
PortLocalizationCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="gr.forthnet.enosis.oli.pojos.PortLocalization"
table="port_localised_details"
lazy = "false"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-PortLocalization.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
/>
<many-to-one
name="port"
class="gr.forthnet.enosis.oli.pojos.OLIPort"
cascade="save-update"
update="true"
insert="true"
column="port"
not-null="true"
/>
<many-to-one
name="lang"
class="gr.forthnet.enosis.oli.pojos.Language"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="lang"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-PortLocalization.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
[b]Language[/b]
<hibernate-mapping
>
<class
name="gr.forthnet.enosis.oli.pojos.Language"
table="languages"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Language.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="code"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Language.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
OLIPort port = new OLIPort();
port.setEnglishName("MPLA");
client.saveOrUpdate(port);
code in setEnglishName(String)
Code:
PortLocalization view = (PortLocalization)getView(lang);
if(view == null) {
view = new PortLocalization(this, lang);
view.setName(name);
views.add(view);
}
Full stack trace of any exception that occurs:Code:
(oli) [16:07:43] ERROR [main] (JDBCExceptionReporter.java:72) - Batch entry 0 insert into languages (code, id) values ( was aborted. Call getNextException() to see the cause.
(oli) [16:07:43] WARN [main] (JDBCExceptionReporter.java:71) - SQL Error: 0, SQLState: 23505
(oli) [16:07:43] ERROR [main] (JDBCExceptionReporter.java:72) - ERROR: duplicate key violates unique constraint "languages_code_key"
(oli) [16:07:43] ERROR [main] (AbstractFlushingEventListener.java:277) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:69)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:150)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1839)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at gr.forthnet.services.hibernate.HiberDatabase.commitTrasaction(HiberDatabase.java:158)
at gr.forthnet.services.hibernate.HiberClient.disconnect(HiberClient.java:90)
at gr.forthnet.enosis.oli.core.Schedule.synchronize(Schedule.java:84)
at gr.forthnet.enosis.oli.core.Schedule.main(Schedule.java:212)
Caused by: Batch entry 0 insert into languages (code, id) values ( was aborted. Call getNextException() to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:107)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 18 more
(oli) [16:07:43] WARN [main] (HiberDatabase.java:163) - commit failed, rolling back
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:69)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:150)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1839)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at gr.forthnet.services.hibernate.HiberDatabase.commitTrasaction(HiberDatabase.java:158)
at gr.forthnet.services.hibernate.HiberClient.disconnect(HiberClient.java:90)
at gr.forthnet.enosis.oli.core.Schedule.synchronize(Schedule.java:84)
at gr.forthnet.enosis.oli.core.Schedule.main(Schedule.java:212)
Caused by: Batch entry 0 insert into languages (code, id) values ( was aborted. Call getNextException() to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:107)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 18 more
Name and version of the database you are using:
PostgreSQL 7.4
In the constructor of PortLocalization I create a new Language object with the given lang code. The problem as u can understand is that when the cascade is happening, an exception occurs since the lang code is already in the db.
Is there a way to handle situations like these? Doing a search I found out that I have to manually perform a select to check if any row returns and replace the current object. Is there some sort of pattern to handle situations like this?