aspose file tools*
The moose likes Object Relational Mapping and the fly likes Is my approach right while inserting a foreign key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Is my approach right while inserting a foreign key?" Watch "Is my approach right while inserting a foreign key?" New topic
Author

Is my approach right while inserting a foreign key?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I am new to hibernate and started practicing from yesterday. I tried with a simple insertion, selection code. Later thought to try a foreign key insertion. This is the code I used to insert records in 2 tables which has a foreign key.

Author.hbm.xmlAuthor.javaBook.javaExample.javaThe records are being successfully inserted.

My problem starts here. Suppose I want to insert another book for the same author. I am using the following approach.
I am taking the author's id and his name, then setting the Author.java POJO class and then setting name and author of the Book.java POJO class and then executing the program. The records are being successfully inserted. The problem is I have to set all details for Author.java POJO. If am not setting author.setName(String) a NULL is being inserted.

How to add a record in the child table without having to change the record in the parent table.

Hope you all understood my problem. Can anyone please shed some light on this issue. Thank you all in advance.


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Is there anyone who can guide me?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is my approach right while inserting a foreign key?