我想用Hibernate映射超类中的通用字段。
我的母亲班是:
@Entity @Table(name = "ParameterValue") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "parameterType", discriminatorType = DiscriminatorType.STRING) public abstract class ParameterValue<C> { private C value; /* HELP NEEDED HERE */ public C getValue() { return value; } public void setValue(C value) { this.value = value; } }
一个子类:
@Entity @DiscriminatorValue(value = "integer") @AttributeOverride(name = "value", column = @Column(name = "intValue")) public class IntegerParameterValue extends ParameterValue<Integer> { }
如您所见,我将覆盖value字段以指定要在数据库中使用的列。我的表ParameterValue由几列组成,每种类型对应一列。
CREATE TABLE `ParameterValue` ( `intValue` int(11) DEFAULT NULL, `doubleValue` double DEFAULT NULL, `stringValue` text ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
但是hibernate抱怨:
ParameterValue.value has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type
好的,但是超类中getValue的良好配置是什么?(我在“需要帮助的地方”发表了评论)
我很确定您不能将单个Java属性映射到三个不同的列。您将必须使用此:
@Entity @Table(name = "ParameterValue") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "parameterType", discriminatorType = DiscriminatorType.STRING) public abstract class ParameterValue<C> { public abstract C getValue(); public abstract void setValue(C value); } @Entity @DiscriminatorValue(value = "integer") public class IntegerParameterValue extends ParameterValue<Integer> { @Column(name = "intValue") private Integer intValue; @Override public Integer getValue() { return intValue; } @Override public void setValue(Integer value) { this.intValue = value; } }