Take the 2-minute tour ×

My hbm.xml file is like this:

<class name="pojopackage.WordhelperWordusage" table="WORDHELPER_WORDUSAGE" schema="SOZANA">
    <composite-id name="id" class="pojopackage.WordhelperWordusageId">
        <key-property name="idwh" type="java.lang.Integer">
            <column name="IDWH" />
        <key-property name="idwu" type="java.lang.Integer">
            <column name="IDWU" />
        <key-property name="type" type="java.lang.Integer">
            <column name="TYPE" />
    <many-to-one name="wordhelper" class="pojopackage.Wordhelper" update="false" insert="false" fetch="select">
        <column name="IDWH" />
    <many-to-one name="wordusage" class="pojopackage.Wordusage" update="false" insert="false" fetch="select">
        <column name="IDWU" />

But I have two POJO classes which are "WordhelperWordusage.java" and "WordhelperWordusageId.java", so i am confused, how should i handle this classes? How to implement? Thanks in advance! Update

I did like this


But its giving me exception like this:

Hibernate: select max(ID) from SOZANA.WORDFORM
Hibernate: select max(ID) from SOZANA.WORDHELPER
Exception in thread "main" org.hibernate.id.IdentifierGenerationException:
ids for this   class must be manually assigned before calling save():   pojopackage.WordhelperWordusage

share|improve this question

3 Answers 3

up vote 0 down vote accepted

Composite PK's are usually mapped to a PK class so that Hibernate can handle them properly. It is the case of the mapping you have posted. Notice that it's not mandatory to use a PK class, you can map those PK attributes in the very same WordhelperWordusage class, if you ommit the class attribute in the <composite-id> element (it's not recommended, though). See this related question for more details: hibernate composite key.

As to implementation given that mapping file, wouldn't it be enough to create the classes with the attributes in the mapping?

package pojopackage;

public class WordhelperWordusage { 

    private WordhelperWordusageId id;
    private Wordhelper wordhelper;
    private Wordusage wordusge;

    // Getters / Setters

    // Implement equals() and hashCode() delegating to id


package pojopackage;

public class WordhelperWordusageId { 
    private Integer idwu;
    private Integer type;

    // Getters / setters

    // Implement equals() and hashCode() consistently, using idwu and type.


Remember to implement hashCode() and equals() in these classes consistently. So that a WordhelperWordusage's hash code is its id's hash code, and they're equal if and only if their id's are equal. For the id class, just hash the two Integer attributes, and compare them for equals().

share|improve this answer
Thanks Xavi for you reply, you helped me previusly i appriciate that, but I have updated the question, could you give me an suggestion? –  Olzhas Oct 3 '12 at 12:47
The error indicates that you're trying to save wordhelper_wordusage without setting an id to it. Before doing session.save(wordhelper_wordusage); you should instantiate a new WordhelperWordusageId() and set its Integer attributes. Aftwerwards set it to wordhelper_wordusage with setId(). –  Xavi López Oct 3 '12 at 12:58
ok I got it, can I make that ID Automatically generated? like an increment –  Olzhas Oct 3 '12 at 13:02
Given the mapping file you provided, you should be setting its type and idwu attributes, ensuring they are unique. –  Xavi López Oct 3 '12 at 13:04
ok, I corrected as you said, its fine but i am getting another error, can you have a look? stackoverflow.com/questions/12706842/… –  Olzhas Oct 3 '12 at 13:07

WordhelperWordusageId.java class is used to build primary key. which has two attributes idwh, and idwu. the combination of this attributes defines the primary key. Hibernate created the object for composite key and override equals and hashCode method. This object used to identify the key for main WordhelperWordusage.java object. so for simplicity consider the object as simple id column attribute, only difference is this is object rather than simple attribute else all rules applied of ID. exception generator scheme.

share|improve this answer

Your Answer


