This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 1609
    
  13

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: foreign key related problem
 
Similar Threads
saving & retreiving image using struts1
Result of HQL for associated objects
problem persisting an object with collections and manytomany relationship
Beginner Problem: son & parent
Deleting Record in a Web Application.