-->
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: Duplicate key or integrity constraint violation
PostPosted: Sun Jun 27, 2004 9:49 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
I use Hibernate 2.1.4 with MySQL (InnoDB tables) 4.0.

I have a relation between Address and Country (non cascade). The Country must already be persisted before it can be set to the Address. Then I want to save the Address. When I do this, I get this error: Duplicate key or integrity constraint violation, message from server: "Duplicate entry 'DE' for key 2"

Here is all the info you need. Maybe someone knows why this error comes?

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="org.liberty.domainobjects.Address"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="ADDRESS_ID"
            type="long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="street"
            type="java.lang.String"
            update="true"
            insert="true"
            column="street"
        />

        <property
            name="postalCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="postalCode"
        />

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
            column="city"
        />

        <property
            name="stateProvince"
            type="java.lang.String"
            update="true"
            insert="true"
            column="stateProvince"
        />

        <many-to-one
            name="country"
            class="org.liberty.domainobjects.Country"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="COUNTRY_ID"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Address.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="org.liberty.domainobjects.Country"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="COUNTRY_ID"
            type="long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="iso3166Alpha2"
            type="java.lang.String"
            update="true"
            insert="true"
            column="iso3166Alpha2"
            unique="true"
        />

        <property
            name="iso3166Alpha3"
            type="java.lang.String"
            update="true"
            insert="true"
            column="iso3166Alpha3"
            unique="true"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
            unique="true"
        />

        <property
            name="dialingCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="dialingCode"
            unique="true"
        />

        <property
            name="iddPrefix"
            type="java.lang.String"
            update="true"
            insert="true"
            column="iddPrefix"
        />

        <property
            name="nddPrefix"
            type="java.lang.String"
            update="true"
            insert="true"
            column="nddPrefix"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Country.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code:
Transaction tx = session.beginTransaction();
CountryData cd = new CountryData(session);
cd.createGermanyCountry();
AddressData ad = new AddressData(session);
ad.createGermanyAddress();
tx.commit();


[code]
public Address createGermanyAddress() throws HibernateException {
if (germanyAddressId != null) {
Address loadedGermanAddress = (Address) session.load(Address.class, germanyAddressId);
return loadedGermanAddress;
}

Address germanyAddress = new Address();
germanyAddress.setStreet("Hans-Schw


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:00 pm 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
I provided you all information. No help?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do not recreate the gesman country, retrieve it from the DB

_________________
Emmanuel


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.