JAVA JPA可选字段(列)

时间:2018-01-18 08:02:53

标签: java database jpa orm entity

我有一个抽象类,它由两个类扩展:

@Setter
@Getter
@MappedSuperclass
public abstract class ConfigElement implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", nullable = false)
    protected Long id;

    @Size(min = 1, max = 50)
    @Column(name = "Code")
    private String code;

    @Basic()
    @Column(name = "ParameterType")
    @Enumerated(EnumType.STRING)
    private ParameterTypeEnum parameterType;

}


@Setter
@Getter
@ToString
public class ConfigRate extends ConfigElement implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "rate", precision=10, scale=2, nullable=false)
    private BigDecimal rate;
}

@ToString
@Setter
@Getter
public class Parameter extends ConfigElement implements Serializable {

    @Size(max = 255)
    @Column(name = "Description")
    private String description;

    @Size(max = 255)
    @Column(name = "CurrentValue")
    private String currentValue;
}

我想要实现的是,字段 parameterType 仅出现在参数表中,并且我不想使其成为可选字段,因此当查询来自 ConfigRate 执行它会忽略 parameterType ,当查询从参数实体执行时,它使用 parameterType 字段,我添加了注释{{ 1}},但它不起作用并抛出此错误:

  

未知栏' t0.ParameterType'在'字段列表'

是否有可能在JPA中实现此行为?

顺便说一句,我正在使用EclipseLink JPA实现。

1 个答案:

答案 0 :(得分:0)

是否可以将注释从paramterType字段移动到Parameter类中的setter方法,如下面的

@Setter
@Getter
@MappedSuperclass
public abstract class ConfigElement implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", nullable = false)
    protected Long id;

    @Size(min = 1, max = 50)
    @Column(name = "Code")
    private String code;

    private ParameterTypeEnum parameterType;

}

@ToString
@Setter
@Getter
public class Parameter extends ConfigElement implements Serializable {

    @Size(max = 255)
    @Column(name = "Description")
    private String description;

    @Size(max = 255)
    @Column(name = "CurrentValue")
    private String currentValue;        

    @Column(name = "ParameterType")
    @Enumerated(EnumType.STRING)
    public ParameterTypeEnum getParameterType() {
        return super.parameterType;
    }
}