File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes foreign key related problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "foreign key related problem" Watch "foreign key related problem" New topic
Author

foreign key related problem

dhaval thakor
Ranch Hand

Joined: Aug 27, 2012
Posts: 53

Hello guys,

I have 2 table in database

first is USERINFO
second is QUERYINFO

I have following situation.

In form user will enter whole info of him and query and submit it.
and
i want to add info of user into USERINFO TABLE and query into QUERYINFO table.

Attributes :

USERINFO
userid - IS AUTO INCREMENTED
name
firstname
....

QUERYINFO
queryid
query
queryanswer
userid
...

so my question is how do add value of userid to QUERYINFO table when newly user info is added to USERINFO...?





chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1649
    
  14

dhaval thakor wrote:so my question is how do add value of userid to QUERYINFO table when newly user info is added to USERINFO...?


Any parent-child relationship implemented by a foreign key basically means the parent must exist before the child can be created, which isn't too hard to figure out - your parents existed before you did, right?

1. Write the user record.
2. Get the ID of the new user. The mechanism for doing this will depend on your set-up e.g. some databases such as Oracle support a RETURNING clause on DML, or your ORM might give you a way to do this.
3. Include the new user ID in your new Query record.
4. Write the Query record.

You could do steps 2 to 4 in a single SQL statement, but as you're posting on a Java forum, I'm guessing you want to use Java.


No more Blub for me, thank you, Vicar.
dhaval thakor
Ranch Hand

Joined: Aug 27, 2012
Posts: 53

This is what i need
Thanks dear...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: foreign key related problem