如何处理复合键hibernate [英] how to handle composite key hibernate

查看:110
本文介绍了如何处理复合键hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < hibernate-mapping> 
< class name =pojopackage.WordhelperWordusagetable =WORDHELPER_WORDUSAGEschema =SOZANA>
< composite-id name =idclass =pojopackage.WordhelperWordusageId>
< key-property name =idwhtype =java.lang.Integer>
< column name =IDWH/>
< / key-property>
< key-property name =idwutype =java.lang.Integer>
< column name =IDWU/>
< / key-property>
< key-property name =typetype =java.lang.Integer>
< column name =TYPE/>
< / key-property>
< / composite-id>
< column name =IDWH/>
< /多对一>
< column name =IDWU/>
< /多对一>
< / class>



但是我有两个POJO类这是WordhelperWordusage.java和WordhelperWordusageId.java,所以我很困惑,我应该如何处理这个类?如何实施?提前致谢!
更新



我确实喜欢这个

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

但是,它给了我这样的例外:

  Hibernate:从SOZANA.WORDFORM 
中选择max(ID)Hibernate:从SOZANA.WORDHELPER中选择max(ID)
线程main中的异常org.hibernate .id.IdentifierGenerationException:必须在调用save()之前手动分配此类的
ids:pojopackage.WordhelperWordusage

解决方案

复合PK通常映射到PK类,以便Hibernate可以正确处理它们。您发布的映射就是这种情况。注意,使用PK类不是强制性的,如果您省略 class ,那么您可以将这些PK属性映射到同一个 WordhelperWordusage 类中。 (code>< composite-id> 元素中的code>属性(尽管如此,不建议这样做)。有关更多详细信息,请参阅以下相关问题: hibernate组合键



对于给定映射文件的实现,用映射中的属性创建类是不够的?

  package pojopackage; 

public class WordhelperWordusage {

private WordhelperWordusageId id;
私人Wordhelper wordhelper;
private wordusage wordusge;

// Getters / Setters

//实现equals()和hashCode()委托给id
}

  package pojopackage; 

public class WordhelperWordusageId {
private Integer idwu;
私人整数类型;

// Getters / setters

//使用idwu和type一致地实现equals()和hashCode()。

$ b

请记住执行 hashCode() equals()一致。因此,一个 WordhelperWordusage 的哈希码是它的id的哈希码,并且当且仅当它们的id相等时它们是相等的。对于id类,只需对两个Integer属性进行哈希处理,然后将它们比较为 equals()


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

解决方案

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

这篇关于如何处理复合键hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆