问题
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
回答1:
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()
.
回答2:
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.
回答3:
This can help you http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component
来源:https://stackoverflow.com/questions/12707978/how-to-handle-composite-key-hibernate