• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

understanding hibernate mapping tutorial

Here i have attached a  hibernate  Event.hbm.xml file.

I have a query here .

note , we have an entry
<id name="id" column="uid" type="long" unsaved-value="null">  

and also we have entry...

<set name="speakers" cascade="all">
            <key column="event_id"/>
            <one-to-many class="Speaker"/>

does that mean hibernate will automatically understand  "id" column  in Event table is a foreign key  "event_id"  column in Speaker table ?

How hibernate will understand that ?i don't see any foreign key tag in the mapping file ....so i am bit confused at this part.

I am not sure , how hibernate will relate these two column .  because i think whenever a value say "1" is saved in "id" field , there will also "1" be saved in event_id column because of this mapping file....right ?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    <class name="Event" table="events">
        <id name="id" column="uid" type="long" unsaved-value="null">
            <generator class="increment"/>
        <property name="name" type="string" length="100"/>
        <property name="startDate" column="start_date"
        <property name="duration" type="integer"/>
        <many-to-one name="location" column="location_id" class="Location"/>
        <set name="speakers" cascade="all">
            <key column="event_id"/>
            <one-to-many class="Speaker"/>
        <set name="attendees" cascade="all">
            <key column="event_id"/>
            <one-to-many class="Attendee"/>

Open in new window

  • 2
1 Solution

that specifies which column is the foreign key for a Speaker
And as it is specified in the Event mapping then it relates to the Event class

cofactorAuthor Commented:
the DDL has been given as below in the tutorial

create table events (uid int ,
name varchar,
start_date date,
duration integer,
location_id int)

create table speakers (uid int,
firstName varchar,
lastName varchar,
event_id int)

but  there is no FK constraint put in this  DDL  .....shall  we need to put constraints here ?
up to you, not necessary to make it work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now