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
|
|