Tuesday, May 15, 2007

Hibernate composite keys with XDoclet

The XDoclet documentation is not cleaver about composite keys. It only shows which annotation must be used (composite-id), but doesn't show where the "key-property" annotation must be placed. After several test I could realize that you should put the key-property in the class that will be used as composite key.


/**
* @hibernate.class table="parent"
*/
public class Parent{
private ParentId id;
private String name;
public void setId(Parent Id){..}
/**
* @hiberante.composite-id
*/
public ParentId getId(){...}
public void setName(String name){...}
/**
* @hibernate.property column="name" type="string"
*/
public String getName(){...}
}
public class ParentId{
private Integer personId;
private Integer code;
public void setPersonId(Integer p){...}
/**
* @hibernate.key-property column="person_id" type="integer"
*/
public Integer getPersonId(){...}
public void setCode(Integer c){...}
/**
* @hibernate.key-property column="code" type="integer"
*/
public Integer getCode(){...}
}

7 comments:

Derek Helbert said...

Thanks, I was looking for an example of this.

JesperBlog said...

thanks, but how the ParentId looks?

is it only plain clean pojo like this:

public class ParentId implements Serializable {
Integer personId;
Integer code;

//getter and setter

}

or need other addition?

JesperBlog said...

ups, sorry, i not read it clear yet, it already written in your post..

cheers

JesperBlog said...

I've already try exactry what you write, i've made Parent and ParentId class exactly same as your write but meet this error: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id) ...

the hbm didn't create the composite-id in the Parent.hbm.xml

please help me franklin!!

fr4gus said...

Hey jesperblog, send me by email the classes you created, looks like something is missing (my sample was not complete) or in a different order.

JesperBlog said...

i never send you my classes because don't have aol account, but i already fixed it, here is the correct one for your example:


/**
* @hibernate.class table="parent"
*/
public class Parent{
private ParentId id;
private String name;
public void setId(Parent Id){..}

/**
* @hiberante.id
*/

public ParentId getId(){...}
public void setName(String name){...}
/**
* @hibernate.property column="name" type="string"
*/
public String getName(){...}
}
public class ParentId{
private Integer personId;
private Integer code;
public void setPersonId(Integer p){...}

/**
* @hibernate.property column="person_id" type="integer"
*/

public Integer getPersonId(){...}
public void setCode(Integer c){...}

/**
* @hibernate.property column="code" type="integer"
*/

public Integer getCode(){...}
}


and it's automatically generate its hbm as composite-id

^-^

Sara Reid said...

I am trying to generate xxx.hbm.xml mapping file thru XDOCLET for composite-id type. I have got three table in my DB schema (Roles, User_Head & User_Det).

nahrungsergänzung