I need to persist a data structure that has value which is either a string, double or date.
Is there a way to do a one-to-one mapping, conditional by table?
I tried this...
implicit def CallHandlerAsResult[R : Defaultable]: AsResult[CallHandler[R]] = new AsResult {
def asResult(c: =>CallHandler[R]) = {
c
Success
}
}
However, hibernate doesn't like this:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
Is there a way to configure hibernate to handle this? Or should I simply re-model the FIELD_CRITERIA table to include 3 optional OneToMany relationships?
答案 0 :(得分:0)
First you may try to make DateCriteriaEntity and NumericCriteriaEntity the owners of the "one-to-one" relation, not the FieldCriteriaEntity. Move the CRITERIA_ID column to tables that correspond to NumericCriteriaEntity and DateCriteriaEntity so that the column will store FieldCriteriaEntity id as foreign key, and use @OneToMany(mappedBy="correspondent field name") in FieldCriteriaEntity instead of your variant.
Consider this article http://uaihebert.com/jpa-onetoone-unidirectional-and-bidirectional/
答案 1 :(得分:0)
我想更好的方法是使用稍微改造你的实体设计。请参阅以下类图。您可以创建一个抽象的CriteriaEntity,它将criteriaId作为主键。请仔细选择子类的继承策略。如果条件实体相对简单,则考虑使用SINGLE_TABLE或移至TABLE_PER_CLASS。
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
您需要重新编写FieldCriteriaEntity
以仅使用一个映射。请参阅以下
@Table(name = "FIELD_CRITERIA")
public class FieldCriteriaEntity implements Identifiable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CRITERIA_KEY", unique = true, nullable = false)
private Long id;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL,optional=true)
@JoinColumn(name="CRITERIA_ID")
private CriteriaEntity criteria;
}
希望这有帮助!