If you want to map a enum in JPA/Hibernate POJO class, so that only specific values can be saved for underlying database column, it is possible now very easily.
public enum StatusEnum {
ACTIVE,
IDLE,
PENDING,
APPROVED;
}
The sample POJO File is given below, have an idea. @Enumerated( javax.persistence.EnumType.STRING ) tells what values you want into database from provided enum type, & hibernate will automatically convert that enum into that specified data type.
@Entity
@Table( name = "Invoice" )
public class Testtable implements java.io.Serializable {
// Fields
private Integer id;
private StatusEnum status;
@Column( name = "status", length = 64 , columnDefinition = "enum(active,pending,idle,approved)")
@Enumerated( javax.persistence.EnumType.STRING )
public StatusEnum getStatus( ) {
return status;
}
public void setStatus( StatusEnum status ) {
this.status = status;
}
// Property accessors
@Id
@Column( name = "id", unique = true, nullable = false )
public Integer getId( ) {
return this.id;
}
public void setId( Integer id ) {
this.id = id;
}
}
