-->
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: detached objects and duplicate key
PostPosted: Thu Sep 29, 2005 9:08 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
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>


PortLocalization
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.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?

_________________
eu:life
http://www.eulife.gr


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 11:12 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I guess the simplest way to avoid this problem is to retrieve the language from the database and assign it to your PortLocalization instead of creating a new one.


Code:
//YOUR BUSINESS CODE

String languageName = "whatever language";
Language language = languageDao.retrieveByName(languageName);
        if(language = null)
            language = new Language(languageName );

PorlLocalization loc = new PortLocatization(language);

//YOUR CONSTRUCTOR

PortLocalization (Language language)
{

//....
}



If you create a new Language, it will always throw an exception because of your constraint.

If you find a more elegant way, let me know. I have not :)

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:50 am 
Regular
Regular

Joined: Tue May 04, 2004 6:15 am
Posts: 51
vgiguere, thanks a lot for taking the time.

Thats what I thought of doing but its always better to get a second opinion.

Credit!

_________________
eu:life
http://www.eulife.gr


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.