休眠复合键 [英] hibernate composite key

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

问题描述

是否有必要将复合 ID 映射到类??

Is it necessary that composite-id should be mapped to class ??

可以这样吗?

<composite-id>
  <key-property=..../>
  <key-property=..../>
</composite-id>

或者应该是

<composite-id class=....>
  <key-property=..../>
  <key-property=..../>
</composite-id>

如果我们有复合键那么那个类应该实现equals()override()方法吗?

should that necessary that if we have composite key then that class should implement equals() and override() method ?

推荐答案

Hibernate 需要能够比较和序列化标识符.因此标识符类必须是可序列化的,并与数据库的复合键相等概念一致地覆盖 hashCode() 和 equals().

Hibernate needs to be able to compare and serialize identifiers. So the identifier class must be serializable, and override hashCode() and equals() consistently with the database's notion of composite key equality.

如果您将复合 ID 映射为实体的属性,则实体本身就是标识符.

If you have a composite id mapped as properties of the entity, the entity itself is the identifier.

第二种方法称为映射复合标识符,其中在 <composite-id> 中命名的标识符属性被称为映射复合标识符.元素在持久类和单独的标识符类上重复

A second approach is called a mapped composite identifier, where the identifier properties named inside the <composite-id> element are duplicated on both the persistent class and a separate identifier class

最后,composite-id 可能是一个组件类.在这种情况下,组件类是标识符类.

Finally, a composite-id may be a component class. In this case the component class is the identifier class.

请注意,强烈建议将 ID 设为单独的类.否则,您将只有非常笨拙的方法来使用 session.get() 或 session.load() 查找您的对象.

Note that it is strongly recommended to have the ID a separate class. Otherwise you will have only very awkward ways to lookup your object using session.get() or session.load().

参考文档的相关部分:

在本例中,复合 ID 被映射为实体的属性.(以下假设您正在定义 Employee 类).

In this example, a composite-id is mapped as properties of the entity. (The following assume you are defining the Employee class).

<composite-id>
    <key-property name="EmployeeNumber"/>
    <key-property name="Dependent"/>
</composite-id>

class EmployeeAssignment implements Serializable
{
    string getEmployeeNumber()
    void setEmployeeNumber( string value )
    string getDepartment()
    void setDepartment( string value )
    boolean equals( Object obj )
    int hashCode()
}

映射的复合 ID:

<composite-id class="EmployeeAssignmentId" mapped="true">
    <key-property name="EmployeeNumber"/>
    <key-property name="Dependent"/>
</composite-id>

class EmployeeAssignment
{
    string getEmployeeNumber()
    void setEmployeeNumber( string value )
    string getDepartment()
    void setDepartment( string value )
}

class EmployeeAssignmentId implements Serializable
{
    string getEmployeeNumber()
    void setEmployeeNumber( string value )
    string getDepartment()
    void setDepartment( string value )
    boolean equals( Object obj )
    int hashCode()
}

作为复合 ID 的组件:

A component as a composite-id:

<composite-id name="Id" class="EmployeeAssignmentId">
    <key-property name="EmployeeNumber"/>
    <key-property name="Dependent"/>
</composite-id>

class EmployeeAssignment
{
    EmployeeAssignmentId getId()
    void setId( EmployeeAssignmentId value )
}

class EmployeeAssignmentId implements Serializable
{
    string getEmployeeNumber()
    void setEmployeeNumber( string value )
    string getDepartment()
    void setDepartment( string value )
    boolean equals( Object obj )
    int hashCode()
}

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

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