hibernate组合键 [英] hibernate composite key

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

问题描述

是否有必要将composite-id映射到class?



是否可以这样?

 <复合-ID> 
< key-property = .... />
< key-property = .... />
< / composite-id>

或应该是

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

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

解决方案

Hibernate需要能够比较和序列化标识符。所以标识符类必须是可序列化的,并且重复hashCode()和equals()与数据库的复合关键字相等概念一致。



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



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

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



请注意,强烈建议将ID设置为单独的类。否则,只有使用session.get()或session.load()才能查找对象的方法非常尴尬。



参考文档的相关章节:





在此示例中,composite-id被映射为实体的属性。 (以下假定您正在定义Employee类)。

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

类EmployeeAssignment实现Serializable
{
字符串getEmployeeNumber()
void setEmployeeNumber(字符串值)
字符串getDepartment()
void setDepartment字符串值)
boolean equals(Object obj)
int hashCode()
}

映射的composite-id:

 < composite-id class =EmployeeAssignmentIdmapped =true > 
< key-property name =EmployeeNumber/>
< key-property name =Dependent/>
< / composite-id>
$ b $ 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()
}

作为组合标识的组件:

 < composite-id name = Idclass =EmployeeAssignmentId> 
< key-property name =EmployeeNumber/>
< key-property name =Dependent/>
< / composite-id>
$ b $ class EmployeeAssignment
{
EmployeeAssignmentId getId()
void setId(EmployeeAssignmentId value)
}
$ b $ class EmployeeAssignmentId implements Serializable
{
string getEmployeeNumber()
void setEmployeeNumber(string value)
string getDepartment()
void setDepartment(string value)
boolean equals(Object obj )
int hashCode()
}


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

can it be like this ?

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

or should be

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

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

解决方案

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.

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

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

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

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

Relevant sections of the Reference Documentation:

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

A mapped composite-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()
}

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

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

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