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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With