-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate not setting foreign key on insert (composite key)
PostPosted: Tue Mar 19, 2013 5:35 am 
Newbie

Joined: Tue Mar 19, 2013 5:30 am
Posts: 1
Hello,

I've posted this question on the StackOverflow already bu without any valid response until now. So maybe someone here can help me to understand the problem.

I have Many-to-one relationship and when I'm trying to insert a value, the foreigh key is not passed. The hibernate doesn't include it's values in the generated SQL query.

Definition of City and LocalizedLocation entities:
Code:
<!-- LocalizedLocation -->
<hibernate-mapping>
   <class name="servicedb.dal.domain.LocalizedLocation" table="localized_location" catalog="DB">
     <composite-id name="id" class="servicedb.dal.domain.LocalizedLocationId">
     <key-property name="localeId" type="int">
       <column name="locale_id" />
     </key-property>
     <key-property name="locationId" type="int">
       <column name="location_id" />
     </key-property>
     </composite-id>
    <many-to-one name="location" class="servicedbcedb.dal.domain.Location" update="false" insert="false" fetch="select">
   <column name="location_id" not-null="true" />
</many-to-one>
<many-to-one name="city" class="servicedb.dal.domain.City" update="false" insert="false" fetch="select" cascade="all">
     <column name="locale_id" not-null="true" />
     <column name="country_code" length="2" not-null="true" />
     <column name="city_id" not-null="true" />
     </many-to-one>   
        <property name="title" type="string">
     <column name="title" length="120" />
     </property>
   </class>
</hibernate-mapping>

<!-- 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="localeId" type="int">
       <column name="locale_id" />
     </key-property>
     <key-property name="countryCode" type="string">
       <column name="country_code" length="2" />
     </key-property>
     <key-property name="id" type="int">
       <column name="id" />
     </key-property>
     </composite-id>
     <property name="name" type="string">
     <column name="name" length="100" not-null="true" />
     </property>
     <set name="localizedLocations" table="localized_location" inverse="true" lazy="true" fetch="select">
     <key>
       <column name="locale_id" not-null="true" />
       <column name="country_code" length="2" not-null="true" />
       <column name="city_id" not-null="true" />
     </key>
     <one-to-many class="servicedb.dal.domain.LocalizedLocation" />
     </set>
   </class>
</hibernate-mapping>


The following code should insert Location and then LocalizedLocation, the LocalizedLocation should have the foreign key pointing to the inserted Location, but for some reason it doesn't.

Code:
Session session = locationDAO.getSession();
session.beginTransaction();

// Location inititalization, the object is correctly populated
session.save(location);

LocalizedLocation localizedLocation = new LocalizedLocation();
localizedLocation.setId(new LocalizedLocationId(locale.getId(), location.getId()));

localizedLocation.setCity(city); // the city already exists on the database, object is not null
localizedLocation.setLocale(locale); // the locale already exusts on the database
localizedLocation.setLocation(location);

session.save(localizedLocation);
session.getTransaction().commit();


After the commit, the generated insert query is the following:
Code:
insert into DB.localized_location (title, description, locale_id, location_id)  values (?, ?, ?, ?)


But it should be:
Code:
insert into DB.localized_location (title, description, locale_id, location_id, city_id, country_code)  values (?, ?, ?, ?, ?, ?)


Does anybody know why the foreign key to the city table is not included in the generated sql insert statement?

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.

Any help is truly appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.