I'm developing an Java-application which stores its data via Hibernate in a database.
One feature of this application is to define templates like types, etc. for reuse. For instance the type has attributes and you can create instances of an type, which has values for the attributes.
The problem is, that I don't know how to ensure that only values for attributes can assigned which the type defines. In my solution there is a redundancy which cause the problem, but I don't know how to remove it.
My current (and problematic) approach looks like this:
@Entity
class Type
{
@Id
@Generated
private Long id;
@OneToMany(mappedBy="type")
private List<Attribute> attributes;
//...
}
@Entity
class Attribute
{
@Id
@Generated
private Long id;
@ManyToOne
private Type type;
//...
}
@Entity
class Instance
{
@Id
@Generated
private Long id;
@ManyToOne
private Type type;
//...
}
@Entity
class AttributeValue
{
@Id
@Embedded
private ResourceAttributValueId id;
@Column(name="val")
private String value;
//...
}
@Embeddable
public class ResourceAttributValueId implements Serializable
{
@ManyToOne
private ResourceStateImpl resource;
@ManyToOne
private ResourceAttributeImpl attribute;
//...
}
There the definition of the type is redundant: Type can be reached via AttributeValue->Attribute->Type and AttributeValue->Instance->Type
Another idea was to use type + attribute name as id of the attribute and instance + attribute name as id of the attribute value, but that doesn't solves my problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…