Java 休眠复合键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2301259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate composite key
提问by Senthilnathan
Is it necessary that composite-id should be mapped to class ??
是否有必要将复合 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()
方法是否有必要?
回答by Lachlan Roche
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.
Hibernate 需要能够比较和序列化标识符。因此标识符类必须是可序列化的,并与数据库的复合键相等概念一致地覆盖 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. In this case the component class is the identifier class.
最后,复合 ID 可能是一个组件类。在这种情况下,组件类是标识符类。
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 设为单独的类。否则,您将只有非常笨拙的方法来使用 session.get() 或 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).
在此示例中,复合 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()
}
A mapped composite-id:
映射的复合 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:
作为复合 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()
}
回答by skaffman
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 alsohave 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.
然后指定的类将用作关键属性的持有者。但是,实体类还必须具有这些属性 - 值存储在实体类和复合 ID 类中。实体类不知道关键类。不是很好,在我看来。
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.
这里,复合键由 class 表示OrderLineId
,它的一个实例存储在id
实体类的字段下。这使实体和密钥之间的分离更加清晰。
回答by Marc
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>