休眠和通用字段映射 [英] hibernate and generic field mapping

查看:103
本文介绍了休眠和通用字段映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的母类是:

<$>

我想用Hibernate映射一个超类的泛型字段。 p $ p $
@Table(name =ParameterValue)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =parameterType ,discriminatorType = DiscriminatorType.STRING)
public abstract class ParameterValue< C>
{
私人C值;

/ *需要帮助* /
public C getValue()
{
返回值;
}

public void setValue(C value)
{
this.value = value;


$ / code $ / pre
$ b $ p一个子类:

  @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具有未绑定类型,并且没有明确的目标实体。解决这个泛型使用问题或者设置一个明确的目标属性(例如@OneToMany(target =)或者使用显式@Type 

好吧,但超类中getValue的好配置是什么?(我在HELP NEEDED HERE中发表了评论)

解决方案

 

@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)
私人整数intValue;

@Override
public Integer getValue(){
return intValue;
}

@Override
public void setValue(Integer value){
this.intValue = value;
}
}


I want to map a generic field in a superclass with Hibernate.

My mother class is :

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

One subclass :

@Entity
@DiscriminatorValue(value = "integer")
@AttributeOverride(name = "value", column = @Column(name = "intValue"))
public class IntegerParameterValue extends ParameterValue<Integer>
{
}

As you can see I override the value field to specify what column to use in the database. My table ParameterValue is composed of several column, one for each type.

CREATE TABLE `ParameterValue` (
    `intValue` int(11) DEFAULT NULL,
    `doubleValue` double DEFAULT NULL,
    `stringValue` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

But hibernate complains that :

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

Ok, but what's the good configuration for getValue in the superclass ? (I've put a comment with "HELP NEEDED HERE")

解决方案

I'm pretty sure you can't map a single Java attribute to three different columns. You will have to use this:

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

这篇关于休眠和通用字段映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆