-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping foreign Keys
PostPosted: Sat Sep 29, 2007 11:48 am 
Newbie

Joined: Sat Sep 29, 2007 11:05 am
Posts: 2
Location: Kenya
Hi,

Am a newbie to Hibernate. Am using Hibernate 3.2 GA and MySql5
I have the table schemas:
School
{id, name, level,location}

Pupil
{adm,schoolId,name,sex,age}

The School source code is:
package school;
public class School {

private int id;
private String name;
private String level;
private String location;
private Set pupils = new HashSet();

/..Getters and
Setters.../
public void addPupils(Pupil p){
pupils.add(p);
}
}
The Pupil source code is :
package school;
public class Pupil {
private int adm;
private int schoolId;
private String name;
private int age;
private char sex;
/..Getters and
Setters.../
}
The mapping files are:
School.hbm.xml

?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 package="school">
<class name="School" table="school">
<id name="id" type="integer">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<property name="level"></property>
<property name="location"></property>
<set name="pupils" table="pupil" inverse="true" cascade="all">
<key column="schoolId" not-null="true"></key>
<one-to-many class="school.Pupil" />
</set>
</class>
</hibernate-mapping>

Pupil.hbm.xml
<?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 package="school">
<class name="Pupil" table="pupil">
<id name="adm">
<generator class="increment"></generator>
</id>
<property name="schoolId">
</property>
<property name="name"></property>
<property name="age"></property>
<property name="sex"></property>
</class>
</hibernate-mapping>

I then execute the following code:
//Open and start session
Session sess = sf.openSession();
sess.beginTransaction();
//Construct objects
School sch = new School();
sch.setName("Mangu Boys High School");
sch.setLevel("Secondary");
sch.setLocation("Thika");
Pupil jm = new Pupil();
jm.setName("James Mwangi");
jm.setAge(14);
jm.setSex('M');
sch.addPupils(jm);
sess.save(sch);
sess.getTransaction().commit();
sess.close();

What I am trying to do is to instantiate both School and Pupil objects. Since a School has many pupils (one-to-many), I created a method for adding Pupil objects to a school object. I then save the School object which goes well without crashing. But one thing is not right, the foreign key schoolId assigned to Pupil is not the same id as the corresponding id for the school.
I obviously have missed something in the mapping files.

I would be very grateful if anyone can assist


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 4:59 am 
Newbie

Joined: Tue Nov 28, 2006 4:34 am
Posts: 5
You should insert this line into Pupil.hbm.xml :
Code:
<many-to-one name="school" column="id" class="School"/>


and you should add a property to Pupil class:
private School school;
+ its getter and setter method

AI
[/code]


Top
 Profile  
 
 Post subject: Mapping Foreign Key
PostPosted: Mon Oct 01, 2007 10:19 am 
Newbie

Joined: Sat Sep 29, 2007 11:05 am
Posts: 2
Location: Kenya
Hi,
Thanks for the response. However, I did as advised but I ran into an error: SEVERE: Unknown column 'id' in 'field list'

But after a couple of hours of debugging, I managed to make it work. Thanks for the insight. Without it I would be seriously stressed!

Anyway, Below is a complete working solution:
Association: A school has many pupils. But a pupil cannot belong to many schools. Hence, the cardinalty is (School) 1:M (Pupil)

Database Schema
Name : Schools
Table: school
{id, name, level,location}

Table:pupil
{adm,schoolId,name,sex,age}


School.java
package school;
public class School{
private int id;
private String name;
private String level;
private String location;
private Set pupils = new HashSet();

/...Setters and
Getters .../
public void addPupils(Pupil p){
pupils.add(p);
}
}

School.hbm.xml
<hibernate-mapping package="school">
<class name="School" table="school">
<id name="id" type="integer">
<generator class="increment" />
</id>
<property name="name" />
<property name="level" />
<property name="location" />
<set name="pupils" table="pupil" inverse="true"
cascade="save-update">
<key column="schoolId" not-null="true" />
<one-to-many class="Pupil" />
</set>
</class>
</hibernate-mapping>

Pupil.java
package school;

public class Pupil {

private int adm;
private int schoolId;
private School school;
private String name;
private int age;
private char sex;

public Pupil(School sch){
this.school = sch;
}

/...Setters and
Getters .../
}

Pupil.hbm.xml
<hibernate-mapping package="school">
<class name="Pupil" table="pupil">
<id name="adm">
<generator class="increment" />
</id>
<property name="schoolId" insert="false" update="false"
not-null="true" />
<property name="name" />
<property name="age" />
<property name="sex" />
<many-to-one name="school" column="schoolId"
class="school.School" not-null="true" />
</class>
</hibernate-mapping>

Create a class call this method and type this in the main method:
public void addSchoolWithPupil(){
//Open and start session
Session sess = sf.openSession();
sess.beginTransaction();
//Construct objects
School sch = new School();
sch.setName("Pioneer High School");
sch.setLevel("Secondary");
sch.setLocation("Maragua");
Pupil jm = new Pupil();
jm.setSchool(sch);
jm.setName("James Mwangi");
jm.setAge(14);
jm.setSex('M');
sch.addPupils(jm);
Pupil pn = new Pupil(sch);
pn.setAge(15);
pn.setName("Peter Ndegwa");
pn.setSex('M');
sch.addPupils(pn);
//Save the objects in the database
sess.save(sch);
sess.getTransaction().commit();
sess.close();
}

I think the above is a good simple example for beginners. I think the Hibernate tutorial should have multiple (redundant) examples. I'll be glad if the above example is integrated into the tutorial


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.