how to enforce @Enumerated(EnumType.STRING) in EmbeddedId

hibernate, java, jpa

Solution

The way to do this is by using the @Enumerated annotation directly on the field:

@Enumerated(EnumType.STRING)

If needed, I guess you could annotate your `JobId` class with `@Embeddable` and then put the `@Enumerated` annotation on your `JobType` field:

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

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

Problem

I have following mapping: ``` 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 } ``` The job status enum column is writen OK with string value in it. But the jobType is written it's ordinal value (0,1,...) and not the name (CLEANER, CRAWLER ...). I want to write into the database the JobType enum name and not it's ordinal() value. Which mapping can I add to the @EmbeddedId to hint this property for jobType column?

Original source