0

My hbm.xml file is like this:

<hibernate-mapping>
<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>
        <key-property name="idwu" type="java.lang.Integer">
            <column name="IDWU" />
        </key-property>
        <key-property name="type" type="java.lang.Integer">
            <column name="TYPE" />
        </key-property>
    </composite-id>
    <many-to-one name="wordhelper" class="pojopackage.Wordhelper" update="false" insert="false" fetch="select">
        <column name="IDWH" />
    </many-to-one>
    <many-to-one name="wordusage" class="pojopackage.Wordusage" update="false" insert="false" fetch="select">
        <column name="IDWU" />
    </many-to-one>
</class>

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

wordhelper_wordusage.save(wordhelper);
wordhelper_wordusage.save(wordusage);
session.save(wordhelper);
session.save(wordusage);
session.save(wordhelper_wordusage);

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

Olzhas
  • 235
  • 2
  • 7
  • 21

3 Answers3

2

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
}

and

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().

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • 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 03 '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 03 '12 at 12:58
  • ok I got it, can I make that ID Automatically generated? like an increment – Olzhas Oct 03 '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 03 '12 at 13:04
  • ok, I corrected as you said, its fine but i am getting another error, can you have a look? http://stackoverflow.com/questions/12706842/object-references-an-unsaved-transient-instance-save-the-transient-instance#comment17158399_12706842 – Olzhas Oct 03 '12 at 13:07
  • Lopez pls, could you have a look on the question, which i have posted in this link. http://stackoverflow.com/questions/12706842/object-references-an-unsaved-transient-instance-save-the-transient-instance#comment17158399_12706842 – Olzhas Oct 03 '12 at 13:52
0

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.

zaffargachal
  • 802
  • 7
  • 21