-->
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.  [ 2 posts ] 
Author Message
 Post subject: Question about Hibernate Mapping many-to-one
PostPosted: Mon May 28, 2007 10:08 pm 
Newbie

Joined: Tue Dec 05, 2006 7:40 am
Posts: 14
Hi all,

I was trying understand Hibernate in my small application I have this db structure(mysql):

create table instructors (
instructor_id int(4) primary key,
first_nm varchar(50),
last_nm varchar(50)
);

create table courses (
course_id int primary key,
instructor_id int ,
course_cd varchar(50),
course_nm varchar(50)
);

alter table courses add foreign key (instructor_id) references instructors(instructor_id)

and my Hibernate maps:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="beans.Instructors">
      <id name="instructor_id" column="instructor_id">
         <generator class="native"></generator>
      </id>
      <property name="first_nm" column="first_nm"></property>
      <property name="last_nm" column="last_nm"></property>
   </class>
</hibernate-mapping>   

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="beans.Courses">
   
      <id name="course_id" column="course_id">
         <generator class="native"></generator>
      </id>
      <many-to-one name="instructor" class="beans.Instructors">
         <column name="instructor_id" not-null="true"></column>
      </many-to-one>
      <property name="course_cd" column="course_cd" />
      <property name="course_nm" column="course_nm" />
   </class>
</hibernate-mapping>   


It works fine but my question is the section:


<many-to-one name="instructor" class="beans.Instructors">
<column name="instructor_id" not-null="true"></column>
</many-to-one>


is correct ? because the instructor can have some courses and one course have only one instructor, right ? then correct should one-to-many in instructor class ? what you think ?

thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 11:28 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi ualex,
Yes, You are right, Use one to many in Instructors HBM for courses. and if you need parent info in any business when you have courses in your hand hen use many to one (Bidirectional)

_________________
Dharmendra Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.