gpt4 book ai didi

hibernate 和通用字段映射

转载 作者:行者123 更新时间:2023-12-02 01:19:21 25 4
gpt4 key购买 nike

我想使用 Hibernate 映射父类(super class)中的通用字段。

我的妈妈类(class)是:

@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>
{
}

如您所见,我覆盖了值字段以指定要在数据库中使用的列。我的表 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

好的,但是父类(super class)中 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;
}
}

关于hibernate 和通用字段映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com