-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Foreign key issue
PostPosted: Thu Sep 01, 2005 9:55 am 
Newbie

Joined: Fri Apr 01, 2005 2:15 am
Posts: 14
Hi all,
I have two tables-1.student(student_id,student_name), 2.info(info_id,student_id,city).Here student_id is the pk of student and fk of info.I want to know that should i mention the pk and fk constrain during my sql create table statement or it is sufficient to mention the mapping tags in the hbm file.If is it in hbm file than how to write this.I have gone through the Hibernate docs but could not find.

I also want that whenever I create a student record ,info record should also be created (student_id should be inserted in both the table).It is possible or not.Please help.

With regards,
Ajse


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 6:56 pm 
Beginner
Beginner

Joined: Mon Aug 01, 2005 3:10 pm
Posts: 22
Hi Ajse,

Yes, the database table create sql should have the PK/FK constraints. Hibernate works on top of that and your Hibernate mappings should also have the corresponding settings for the relationships.

You need to spend some time reading the docs. You will find that the section on Associations explains what you are trying to do: http://www.hibernate.org/hib_docs/v3/re ... sociations

As for automatic creation of records for related tables, Hibernate supports it with specific configuration settings in the mappings. Look for the "cascade" attribute. It's setting can control how your insert/updates cascade through an object graph - its use depends on application specific needs.

Mrtz

_________________
* Please rate my posting if it answered your question, thanks! *


Top
 Profile  
 
 Post subject: Thanks mrtz
PostPosted: Fri Sep 02, 2005 1:06 am 
Newbie

Joined: Fri Apr 01, 2005 2:15 am
Posts: 14
Hi mrtz,
Thanks for your reply.It was really helpful.But I could not find how should
I rate your reply.
with regards,
Ajse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 6:00 am 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
You need to map one-to-many relationship between Student and Info entities. In Student mapping you may specify such relationship as following:

Code:
    <class name="com.my.domain.Student"
        table="STUDENT">

        <id
            name="id"
            column="STUDENT_ID"
            type="long">
        </id>
        ... other fields mapping goes here

        <set
            name="infos"
            lazy="false"
            cascade="save-update"
            sort="unsorted">

            <key column="STUDENT_ID" />
            <one-to-many class="com.my.domain.Info" />

        </set>

       ....the rest of you mapping file

    </class>



In this case, all you need to do is to create Student, set its properties, also create Info object, and add it to infos set:

Code:

Student student = new Student();
student.setName("name");
...
Info info = new Info();
info.setInfoDetails(details);
//obtain set either from Student.getInfos() or create new one
Set infos = new HashSet();
infos.add(info);
//update infos set in Student
student.set(infos);
//now lets' save data
session.save(student);


All nested objects that has primary key value equals to 'unsaved-value' Hibernate will try to persist by inserting new rows in correspondent tables.
This is done by specifying cascade="save-update" in mapping file

Hope this would help to shed the light on Hibernate magic =)
Feel free to ask for details


Top
 Profile  
 
 Post subject: to rate
PostPosted: Thu Sep 08, 2005 3:07 pm 
Beginner
Beginner

Joined: Mon Aug 01, 2005 3:10 pm
Posts: 22
Hi Asje,
You should be able to click on the link below my posting such as:
Post rating: Please rate this answer to give credits to the poster! Did it solve the problem? Yes / No
-Mrtz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.