-->
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 mapping
PostPosted: Mon Mar 23, 2009 8:48 am 
Newbie

Joined: Wed Mar 18, 2009 3:28 am
Posts: 11
i have two tables employee with fields empid(primary key),name and age
and dependents table with fields depid(primary key),empid(foreign key),dependent name.

can you please provide the mapping file (hbm.xml)for the same.

thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 12:22 pm 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Hi Vibhuti,

May be you shud try to do somethng yourself and then if you get any issues.. post it... we can help u out...

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: my hbm file is here:-
PostPosted: Tue Mar 24, 2009 1:29 am 
Newbie

Joined: Wed Mar 18, 2009 3:28 am
Posts: 11
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class name="com.iwms.hibernate.JavaBeans.TestFK.EmployeeDetails" table="employee" >
<id name="empId" type="java.lang.String" column="empid" ></id>
<property name="name" type="java.lang.String" column="name" length="30" />
<property name="age" type="java.lang.Integer" column="age" length="2" />

</class>

<class name="com.iwms.hibernate.JavaBeans.TestFK.DependentDetails" table="dependents">
<id name="depId" type="java.lang.String" column="depid" ></id>
<property name="empId" type="java.lang.String" column="empid" length="30" />
<property name="name" type="java.lang.String" column="name" length="30" />

</class>



</hibernate-mapping>




class file for dependent table:-

package com.iwms.hibernate.JavaBeans.TestFK;

import java.io.Serializable;

public class DependentDetails implements Serializable{

private static final long serialVersionUID = 1L;
private String empId;
private String depId;
private String name;
public String getDepId() {
return depId;
}
public void setDepId(String depId) {
this.depId = depId;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}



class file for employee table:-

package com.iwms.hibernate.JavaBeans.TestFK;

import java.io.Serializable;

public class EmployeeDetails implements Serializable{

private static final long serialVersionUID = 1L;
private String empId;
private String name;
private int age;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 3:14 am 
Beginner
Beginner

Joined: Fri Feb 13, 2009 5:27 am
Posts: 36
Location: India
HI Vibhuti,,

change ur mapping file..

<id name="empId" type="java.lang.String" column="empid" >
<generator class="native">
</generator>
</id>


and tell me what is the relationship between ur these two classes(for foreign key)...then wil tell u the exact solution....

_________________
parag


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 3:19 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Quote:
<class name="com.iwms.hibernate.JavaBeans.TestFK.DependentDetails" table="dependents">
<id name="depId" type="java.lang.String" column="depid" ></id>
<property name="empId" type="java.lang.String" column="empid" length="30" />
<property name="name" type="java.lang.String" column="name" length="30" />

</class>


Here you have mapped the empId as String. But it actually points to an Employee object. So in OO paradigm, the property should be of type EmployeeDetails. So the mapping should be like:

Code:
<class name="com.iwms.hibernate.JavaBeans.TestFK.DependentDetails" table="dependents">
    <id name="depId" type="java.lang.String" column="depid" >
        <generator class="native"/>
    </id>
    <many-to-one name="employee" class="com.iwms.hibernate.JavaBeans.TestFK.EmployeeDetails" cascade="none" column="empid" />
    <property name="name" type="java.lang.String" column="name" length="30" />
</class>


public class DependentDetails implements Serializable{

    private static final long serialVersionUID = 1L;
    private EmployeeDetails employee;
    private String depId;
    private String name;

    .............
    .............

    public EmployeeDetails getEmployee() {
        return employee;
    }

    public void setEmployee(EmployeeDetails employee) {
        this.employee= employee;
    }
    .............
    .............
}

_________________
Regards,
Litty Preeth


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.