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