-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping a table to itself having parent child relationship
PostPosted: Fri Nov 23, 2007 5:48 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Hi all,
My table structure is like this
table(object_id(PK),name,parent_id)
The enteries in the table are as folllows
1 abc 0
2 xyz 1
3 dsf 2

In the data given above the parent_id contains the primary key of the row except the first row.Now what my requirement is that i should be able to retrieve the parent object from a given object. i,e. If i have a retreived row represented as object ,i should be able to retrieve parent object like this object.gatparentObject.objectId


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 6:01 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Try this:

I have created Person class.

package com.shyam.bean;

import java.io.Serializable;

/**
* @hibernate.class
* @author MutchaS
*
*/
public class Person implements Serializable {
private static final long serialVersionUID = 1L;

private Long id;
private String name;
private Person spouse;

/**
* @hibernate.id
* @return
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

/**
* @hibernate.property
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

/**
* @hibernate.many-to-one cascade="all" unique="true"
* @return
*/
public Person getSpouse() {
return spouse;
}
public void setSpouse(Person spouse) {
this.spouse = spouse;
}
}

Here is the mapping:

<?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.shyam.bean.Person"
>

<id
name="id"
column="id"
>

</id>

<property
name="name"
column="name"
>

</property>

<many-to-one
name="spouse"
unique="true"
cascade="all"
>

</many-to-one>

</class>

</hibernate-mapping>


Now when you query for person.getSpouse(), it gives spouse id (if one exist).

You can try on similar lines. Let me know if you have any issues.

Thanks
Shyam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 1:08 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Hi Shyam,ThankYou.
Now when i try your solution,it gives the exception

ERROR: 11-26-2007 10:32:43 org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:72) - Invalid column name 'columnName'.

Do you have any clues why this is happening


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 6:30 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Hi,
Can you send me your code where this exception is given? I have executed the sample code pasted below. It worked fine.

1. There is typo in the Person.hbm.xml. Change the column name to 'name' field as:

<property
name="name"
column="name"
>

2. Insert two records into Person table.
insert into person(id, name) values(1, 'husband')
insert into person(id, name, spouse) values(2, 'wife', 1)

3. Execute the PersonDAO.
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class PersonDAO {
public static void main(String args[]){
Session _session = HibernateUtil.currentSession();
//System.out.println("_session: "+_session);

Person _person = (Person)_session.createCriteria(Person.class)
.add(Restrictions.eq("id", new Long(1)))
.uniqueResult();
Person _spouse = _person.getSpouse();
System.out.println("_person: "+_person);
System.out.println("_spouse: "+_spouse);

_person = (Person)_session.createCriteria(Person.class)
.add(Restrictions.eq("id", new Long(2)))
.uniqueResult();
_spouse = _person.getSpouse();
System.out.println("_person: "+_person);
System.out.println("_spouse: "+_spouse);

}
}

4. Result should be:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.spouse as spouse0_0_ from Person this_ where this_.id=?
_person: com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1]
_spouse: null
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.spouse as spouse0_0_ from Person this_ where this_.id=?
_person: com.shyam.hibernate.Person@19da4fc[spouse=com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1],name=wife,id=2]
_spouse: com.shyam.hibernate.Person@1e228bc[spouse=<null>,name=husband,id=1]

Thanks


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