如何在EmbeddedId中强制执行@Enumerated(EnumType.STRING)

时间:2013-03-28 13:57:34

标签: java hibernate jpa

我有以下映射:

public enum JobType {
    CLEANER,
    CRAWLER, ...
 }


public class JobId implements Serializable {
    private int accId;
    private String env;
    private JobType jobType;
 }

@SuppressWarnings("serial")
@Entity
@Table(name = "jobs")
public class JobExecution {

    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name = "accId", column = @Column(name = "acc_id")),
        @AttributeOverride(name = "env", column = @Column(name = "env")),
        @AttributeOverride(name = "jobType", column = @Column(name = "agg_type")) })
    private JobId jobId;

    @Enumerated(EnumType.STRING)
    @Column(name = "Job_STATUS")
    private JobStatus jobStatus;

    //...  other staff 
}

作业状态枚举列写入正常,其中包含字符串值。

但是jobType写的是序数值(0,1,...),而不是名字(CLEANER,CRAWLER ......)。

我想在数据库中写入JobType枚举名称而不是它的序数()值。

我可以在@EmbeddedId中添加哪个映射来为jobType列提示此属性?

1 个答案:

答案 0 :(得分:1)

这样做的方法是直接在字段上使用@Enumerated annotation

@Enumerated(EnumType.STRING)

如果需要,我猜您可以使用JobId@Embeddable课程添加注释,然后将@Enumerated注释放在JobType字段上:

@Embeddable
public class JobId implements Serializable {
    private int accId;
    private String env;

    @Enumerated(EnumType.STRING)
    private JobType jobType;
}