-->
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: many-to-one mapping with foreign key attribute (not entity)
PostPosted: Thu Jun 03, 2004 7:04 am 
Newbie

Joined: Thu Jun 03, 2004 6:25 am
Posts: 2
Hi,

I didnt see a possibility of many-to-one mapping on an attribute using Hibernate (v 2.1).

I have Person & Relation entities with bidirectional mapping
Person -> Relation ( one-to-many)
Relation->Person (many-to-one)

Relation POJO has Person's ID as the attribute. I want to map this attribute to Person POJO's ID using many-to-one relationship.
But many-to-one mapping doesnt take "type" but only "class"
( Note: I dont want to map the entire Person in my Relation POJO).

The Relation.hbm.xml that I want to map looks like this

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Relation" table="PERSON_RELATION">
<id name="id" column="PERSON_RELATION_ID">
<generator class="sequence">
<param name="sequence">sqidx</param>
</generator>
</id>
<many-to-one name="personId" column="PERSON_ID"/>
<property name="firstName" column="FIRST_NAME" />
<property name="lastName" column="LAST_NAME" />
</class>
</hibernate-mapping>

Person.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="sequence">
<param name="sequence">sqidx</param>
</generator>
</id>
<set name="relations" lazy="true" inverse="true">
<key column="PERSON_ID"/>
<one-to-many class="com.mckinsey.peoplenet.model.Relation" />
</set>
</class>
</hibernate-mapping>
Relation.java:

public class Relation {

private long id;
/**
* @hibernate.id
* column="PERSON_RELATION_ID"
* generator-class="sequence"
* @hibernate.generator-param
* name="sequence"
* value="sqidx"
*/
public long getId() {
return id;
}
public void setId(long newId) {
this.id = newId;
}
// Person ID is mapped not the Person entity
private long personId;
/**
* @hibernate.many-to-one
* column="PERSON_ID"
*/
public long getPersonId() {
return personId;
}
public void setPersonId(long newPersonId) {
this.personId= newPersonId;
}

private String firstName;
/**
* @hibernate.property
* column="FIRST_NAME"
*/
public String getFirstName() {
return firstName;
}
public void setFirstName(String str) {
this.firstName = str;
}
private String lastName;
/**
* @hibernate.property
* column="LAST_NAME"
*/
public String getLastName() {
return lastName;
}
public void setLastName(String str) {
this.lastName = str;
}
}

I dont see the possibility to make it working using hibernate.
The below mapping when run says that the "long" (using reflection) is not a mapped class
(Its looking for class definition instead of basic type)
Please let me know If it is possible to do such mapping?

_________________
Murali Krishna


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 2:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
map it as a property, not a many-to-one.
Not this is clearly not OO and more relational => not very well suited for an ORM tool

_________________
Emmanuel


Top
 Profile  
 
 Post subject: it worked
PostPosted: Mon Jun 14, 2004 10:51 am 
Newbie

Joined: Thu Jun 03, 2004 6:25 am
Posts: 2
thanks for the update.I was able to get it work with a mapping as property.

_________________
Murali Krishna


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.