This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes how to insert a row with FK ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to insert a row with FK ?" Watch "how to insert a row with FK ?" New topic
Author

how to insert a row with FK ?

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Suppose I have following two tables

User (ID ->PK, name, address)
Dept (ID -->PK, User_ID-->FK, dept_name)

where the ID for "User" and "Dept" are Oracle sequence ID. Question is --

When I do an insert in "Dept" table, it involves "User_ID" which is the PK of the other table, how can I do this in Hibernate ? Of course, I know which "name" and "address" I am using for the insert.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Usually it is the User that has a FK to Dept, but anyway:
the FK field gets inserted just like the other fields.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

When you map relationships between objects and use cascade options, basically Hibernate is smart enough to know that if you create two new objects, one for user and one for dept, and associate the two objects together, then call save it will know that it needs to first insert the record that will have the id to put into the fk field for the other insert, and it works the same where one will be new and the other is just an update.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Originally posted by Mark Spritzler:
When you map relationships between objects and use cascade options, basically Hibernate is smart enough to know that if you create two new objects, one for user and one for dept, and associate the two objects together, then call save it will know that it needs to first insert the record that will have the id to put into the fk field for the other insert, and it works the same where one will be new and the other is just an update.

Mark


Mark,

1. shall I do save(person) first followed by save(dept) because dept holds a FK pointing to person ?

2. the second question may be stupid as I am newbie in hibernate -- when we save(person), we save a "person" object that does NOT include its ID information, correct ? I think the iD is handled by the datbase key.

Similarly, when we save(dept), we only save a "dept" object that does NOT include its ID, and person_ID, correct ? In other words, when we use hibernate, we ONLY need to focus on our natural fields, all of the system generated ID(s) will be automatically generated and inserted by database, right ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Raj Ohadi:


Mark,

1. shall I do save(person) first followed by save(dept) because dept holds a FK pointing to person ?

2. the second question may be stupid as I am newbie in hibernate -- when we save(person), we save a "person" object that does NOT include its ID information, correct ? I think the iD is handled by the datbase key.

Similarly, when we save(dept), we only save a "dept" object that does NOT include its ID, and person_ID, correct ? In other words, when we use hibernate, we ONLY need to focus on our natural fields, all of the system generated ID(s) will be automatically generated and inserted by database, right ?



1) No you only need to call save on one of the objects, and Hibernate will know what to do in terms of inserting into both tables and which is supposed to be inserted first. But as long as the relationships are mapped correctly.

2) If you map your Ids with "native" or "increment" or "sequence" those generally mean that the database will handle the generation of Ids for you. only if you set the mapping to "application" will it require you to program the id in.

Mark
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Originally posted by Mark Spritzler:



1) No you only need to call save on one of the objects, and Hibernate will know what to do in terms of inserting into both tables and which is supposed to be inserted first. But as long as the relationships are mapped correctly.


Mark


Mark, still bit confused here. For example

Person(111, "John" , "123 Main St")
Person(112, "Steve", "333 Tulane Ave")

Dept(1, 111, "Chemistry")
Dept(2, 112, "History")
...

Now I want to insert a row to "Dept" table as follows

Dept(10, 111, "English")

I mean to tie "person_id" of 111 ("John") with this new row. If I simply do

Dept d = new Dept("English");
save(d);

It is not going to work I guess as it doesn't know which person_id I want to associate with. So how should I do this to insert a row to "dept" simply ?

Thanks.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I think you are still trying to think in terms of database.

When you are working with Objects, you need to think in the object world. Hibernate is there to handle the mismatch for you so in the Object world you can think in Objects, and in the Database world you think in Database terms.

So in the object world, you should never have to think about ids, just relationships/associations between objects.

In your example you just wrote, it looks like you are manually creating objects and putting in the value for the id attribute, which you should only need to do if you set the id generation to "application" Which is rare to need to do that.

So I would have


Person p = new Person("Steve", "333 Tulane Ave");
Dept d = Dept("English");
d.setPerson(p);
save(d);

Since the association is set through Objects between the English department and Steve the person, then I allow Hibernate to see that it needs to first Insert a record for Steve into the database, and the database will assign the ID for Steve, then when it goes to insert the record for the English department, Hibernate already has Steve's id to put into the FK field for the English department record.

Mark
 
wood burning stoves
 
subject: how to insert a row with FK ?