• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to insert a row with FK ?

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually it is the User that has a FK to Dept, but anyway:
the FK field gets inserted just like the other fields.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic