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: Inserts not maintaining FK references in <join> mappin
PostPosted: Fri Jun 20, 2008 6:37 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Hi all,

I have a legacy schema with two tables which I am trying to map to a single class using <join> construct. The data is read-only, but I would like to insert some test data during development. The FK references are not maintained during inserts; please help.

Hibernate version: 3.2.6.ga

Mapping documents:
Code:
<class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">...</id>
    <join table="ADDRESS">
        <key column="ADDRESS_ID"/>
        <property name="address"/>
        ...
    </join>
    ...
</class>


Code between sessionFactory.openSession() and session.close():
Code:
txn = session.beginTransaction();
Person p = new Person();
p.setAddress("Test Address");
session.save(p);
txn.commit();


Name and version of the database you are using:
HSQL 1.8.0.7

The generated SQL (show_sql=true):
Code:
Hibernate: insert into person (id) values (null)
Hibernate: call identity()
Hibernate: insert into address (address, address_id) values (?, ?)

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:20 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
With which part do you need help, and what is the problem you are facing?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 11:44 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Notice the SQL queries:
Code:
Hibernate: insert into person (id) values (null)
Hibernate: insert into address (address, address_id) values (?, ?)


I am expecting something like:
Code:
Hibernate: insert into address (address, address_id) values (?, ?)
Hibernate: insert into person (address_id, id) values (?, null)


I want Hibernate to set up the FK reference to the joined table automagically when I save the entity.

(As a workaround for now after session.save(), I do a session.flush() and then issue my own SQL update to the DB creating the FK reference using the java.sql.Connection obtained from session.connection().)

_________________
Thanks,
Binil Thomas


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.