休眠复合键

发布于 2024-08-22 10:27:43 字数 383 浏览 13 评论 0原文

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

可以这样吗?

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

或者应该有

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

必要,如果我们有复合键,那么该类应该实现 equals() 和 override() 方法?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-08-29 10:27:43

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

如果您有一个映射为实体属性的复合 ID,则实体本身就是标识符。

第二种方法称为映射复合标识符,其中在内命名的标识符属性可以被称为映射复合标识符。元素在持久类和单独的标识符类上都是重复的。

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

请注意,强烈建议将 ID 放在单独的类中。否则,您将只能以非常尴尬的方式使用 session.get() 或 session.load() 查找对象。

参考文档的相关部分:

在此示例中,复合 ID 被映射为实体的属性。 (以下假设您正在定义 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()
}

映射的复合 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 的组件:

<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 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()
}
∝单色的世界 2024-08-29 10:27:43

两者皆有可能。如果您使用

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

那么不需要单独的类来表示密钥。 ID 值取自实体本身的属性。

如果使用

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

那么指定的类将用作关键属性的持有者。但是,实体类还必须具有这些属性 - 这些值同时存储在实体类和复合 ID 类中。实体类不知道键类。在我看来,不太好。

有一个更好的第三种方法,在文档中描述 此处

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

此处,复合键由类 OrderLineId 表示,该类的实例存储在字段 id 下实体类。这使得实体和键之间的分离更加清晰。

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.

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. 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. This keeps the separation between entity and key much cleaner.

要走干脆点 2024-08-29 10:27:43

如果您有一个包含与其他实体的关系的复合键,请按如下所示操作:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文