简体   繁体   English

休眠复合键

[英]hibernate composite key

Is it necessary that composite-id should be mapped to class ?? 是否有必要将composite-id映射到类?

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 ? 如果有必要,如果我们有复合键,那么该类应该实现equals()override()方法?

Hibernate needs to be able to compare and serialize identifiers. Hibernate需要能够比较和序列化标识符。 So the identifier class must be serializable, and override hashCode() and equals() consistently with the database's notion of composite key equality. 因此标识符类必须是可序列化的,并且与数据库的复合键相等概念一致地覆盖hashCode()和equals()。

If you have a composite id mapped as properties of the entity, the entity itself is the identifier. 如果您将复合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. 最后,composite-id可以是组件类。 In this case the component class is the identifier class. 在这种情况下,组件类是标识符类。

Note that it is strongly recommended to have the ID a separate class. 请注意,强烈建议将ID作为单独的类。 Otherwise you will have only very awkward ways to lookup your object using session.get() or session.load(). 否则,您将只使用session.get()或session.load()查找对象的方法非常笨拙。

Relevant sections of the Reference Documentation: 参考文档的相关部分:

In this example, a composite-id is mapped as properties of the entity. 在此示例中,composite-id被映射为实体的属性。 (The following assume you are defining the Employee class). (以下假设您正在定义Employee类)。

<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:

<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的组件:

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

Both are possible. 两者都有可能。 If you use 如果你使用

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

Then no separate class is required to represent the key. 然后,不需要单独的类来表示密钥。 The ID values are taken from the properties of the entity itself. ID值取自实体本身的属性。

If you use 如果你使用

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

Then the specified class will be a used as a holder for the key properties. 然后,指定的类将用作键属性的持有者。 However, the entity class must also have these properties - the values are stored both in the entity class and the composite ID class. 但是,实体类必须具有这些属性 - 值存储在实体类和复合ID类中。 The entity class has no knowledge of the key class. 实体类不知道密钥类。 Not very nice, in my opinion. 在我看来,不是很好。

There is a nicer 3rd approach, described in the docs here : 有一个更好的第三种方法,在这里的文档中描述:

<composite-id name="id" class="OrderLineId">
    <key-property name="lineId"/>
    <key-property name="orderId"/>
    <key-property name="customerId"/>
</composite-id>

Here, the composite key is represented by the class OrderLineId , an instance of which is stored under the field id in the entity class. 这里,复合键由OrderLineId类表示,其实例存储在实体类中的字段id下。 This keeps the separation between entity and key much cleaner. 这使实体和密钥之间的分离更加清晰。

If you have a composite key that contains relationships to other entities, do it like this: 如果您有一个包含与其他实体的关系的复合键,请执行以下操作:

<composite-id>
    <key-many-to-one name="employee" column="FK_EMPLOYEE" entity-name="net.package.name.Employee" />
    <key-many-to-one name="department" column="FK_DEPARTMENT" entity-name="net.package.name.Department" />
</composite-id>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM