aumanets
aumanets

Reputation: 3833

Hibernate doesn't insert foreign key

I have Many-to-one relationship and when I'm trying to insert a value, the foreigh key is not passed.

Enitnty Country

<hibernate-mapping>
<class name="servicedb.dal.domain.Country" table="Country" catalog="DB">
    <composite-id name="id" class="servicedb.dal.domain.CountryId">
        <key-property name="countryCode" type="string">
            <column name="countryCode" length="2" />
        </key-property>
        <key-property name="localeId" type="int">
            <column name="localeId" />
        </key-property>
    </composite-id>
    <many-to-one name="locale" class="servicedb.dal.domain.Locale" update="false" insert="false" fetch="select">
        <column name="localeId" not-null="true" />
    </many-to-one>
    <property name="name" type="string">
        <column name="name" length="60" not-null="true" />
    </property>
    <set name="cities" table="City" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="countryCode" length="2" not-null="true" />
            <column name="localeId" not-null="true" />
        </key>
        <one-to-many class="servicedb.dal.domain.City" />
    </set>
</class>

Entity City

<hibernate-mapping>
    <class name="servicedb.dal.domain.City" table="City" catalog="DB">
        <composite-id name="id" class="servicedb.dal.domain.CityId">
            <key-property name="id" type="int">
                <column name="id" />
            </key-property>
            <key-property name="localeId" type="int">
                <column name="localeId" />
            </key-property>
        </composite-id>
        <many-to-one name="country" class="servicedb.dal.domain.Country" update="false" insert="false" fetch="select">
            <column name="countryCode" length="2" not-null="true" />
            <column name="localeId" not-null="true" />
        </many-to-one>
        <property name="name" type="string">
            <column name="name" length="100" not-null="true" />
        </property>
        <set name="localizedLocations" table="LocalizedLocation" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="cityId" />
                <column name="localeId" not-null="true" />
            </key>
            <one-to-many class="servicedb.dal.domain.LocalizedLocation" />
        </set>
    </class>
</hibernate-mapping>

When I create the City entity, I'm setting the Country correctly and the countryCode is not null. But the generated query look like the following:

insert into Db.City (name, id, localeId) values (?, ?, ?)

But it should be:

insert into Db.City (name, id, localeId, countryCode) values (?, ?, ?, ?)

And the hibernate throws the following exception

org.hibernate.exception.GenericJDBCException: Field 'countryCode' doesn't have a default value

I hope that the provided information is sufficient to understand the cause of the error. If not, please ask specifically for additional information.

I'm also using the eclipse and reveng.xml to reverse engineer the database, so my hbm files are auto generated and I'm not using EJB3 annotations.

EDIT: Posted the full mapping of the Country and City entities.

Upvotes: 1

Views: 1560

Answers (1)

overmeulen
overmeulen

Reputation: 1158

The update="false" insert="false" in the many-to-one of your City mapping applies to both countryCode and localeId. Since localeId is also mapped in your composite-id it is used in the generated queries but that's not the case for countryCode ...

Upvotes: 1

Related Questions