@Any注解定义了一个多态关联到来自多个表的类。这种类型的映射 总是需要多个列。第一列保存关联实体的类型。其余的 列保存标识符。为这种关联指定外键约束是不可能的,所以这当然不意味着映射(多态)关联的常用方式。您只能在非常特殊的情况下使用此 (例如,审计日志,用户会话数据等)。 @Any注释描述了包含元数据信息的列。要将 元数据信息的值与实际实体类型链接起来,请使用@AnyDef和@AnyDefs注释。
@Any(metaColumn = @Column(name = "property_type"), fetch=FetchType.EAGER)
@AnyMetaDef(
idType = "integer",
metaType = "string",
metaValues = {
@MetaValue(value = "S", targetEntity = StringProperty.class),
@MetaValue(value = "I", targetEntity = IntegerProperty.class)
})
@JoinColumn(name = "property_id")
public Property getMainProperty() {
return mainProperty;
}
idType代表目标实体标识符属性类型和metaType元数据类型(通常是String)。 请注意,@AnyDef可以相互关联和重用。建议将它作为一个包元数据放在这个 的案例中。
//on a package
@AnyMetaDef(name="property"
idType = "integer",
metaType = "string",
metaValues = {
@MetaValue(value = "S", targetEntity = StringProperty.class),
@MetaValue(value = "I", targetEntity = IntegerProperty.class)
})
package org.hibernate.test.annotations.any;
//in a class
@Any(metaDef="property", metaColumn = @Column(name = "property_type"), fetch=FetchType.EAGER)
@JoinColumn(name = "property_id")
public Property getMainProperty() {
return mainProperty;
}
@ManyToAny允许多态关联到多个表中的类。这种映射总是需要多个列。第一列保存关联实体的类型。其余的列 保存该标识符。为这种关联指定外键约束是不可能的,所以这当然不是通常映射(多态)关联的方式。您只能在特殊情况下(例如,审计日志,用户会话数据等)使用此功能。
@ManyToAny(
metaColumn = @Column(name = "property_type"))
@AnyMetaDef(
idType = "integer",
metaType = "string",
metaValues = {
@MetaValue(value = "S", targetEntity = StringProperty.class),
@MetaValue(value = "I", targetEntity = IntegerProperty.class) })
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@JoinTable(name = "obj_properties", joinColumns = @JoinColumn(name = "obj_id"),
inverseJoinColumns = @JoinColumn(name = "property_id"))
public List<Property> getGeneralProperties() {
源:Hibernate Annotations Reference Guide 3.4.0GA
希望它能帮助!
根据Bill Karwin的书,我想指出这种做法被认为是SQL反模式。 – atorres 2015-10-06 14:26:18