我有下表...
Object1
-------
id
...
Object2
-------
id
...
AttributeValue
--------------
id
attribute_id
object_id
value
Attribute
---------
id
name
type
...和实体类
@Entity
@Table(name = "Attribute")
public class Attribute {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "type")
private String type;
}
@Entity
@Table(name = "AttributeValue")
public class AttributeValue {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "attribute_id")
private Long attributeId;
@Column(name = "object_id")
private Long objectId;
@Column(name = "value")
private String value;
}
@Entity
@Table(name = "Object1")
public class Object1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// ...
// how to annotate to get all matching attribute values?
private Set<AttributeValue> values;
}
我想让休眠状态的values
实例变量充满所有具有相应AttributeValue
和属性类型object_id
的{{1}}。
如果只是关于object1
的标准,我会写例如。
object_id
但是在这种情况下,它还将填写类型为@JoinColumn(insertable = false, updatable = false, name = "object_id")
private Set<AttributeValue> values;
等的值。
所以我的问题是:这种语义是否可以在Hibernate中表达?如果可以,如何表达?
编辑:我想强调一下,目标是要有多个没有共同层次结构的对象(这里是object2
,Object1
,... Object2
),但都共享具有属性的功能。所有对象的属性将驻留在 one 表中,该表由某种区分符(此处示例为ObjectN
)加以区分。
答案 0 :(得分:0)
我认为该对象必须是:
@Entity
@Table(name = "AttributeValue")
public class AttributeValue {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "attribute_id")
private Long attributeId;
@ManyToOne
@JoinColumn(name="object_id", nullable=false)
private Object1 object1;
@Column(name = "value")
private String value;
}
@Entity
@Table(name = "Object1")
public class Object1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="object1")
private Set<AttributeValue> values;
}
休眠将仅在AttributeValue表上生成object_id列。