In my Hibernate application, I want to map Java enum to Postgresql enum and vice-versa.
My application is using apache-thrift,hibernate/JPA(for ORM model),postgresql DB.
Postgresql DB have defined enum using command
CREATE TYPE status_enum AS ENUM ('UNDER-CONSTRUCTION', 'CLOSED', 'OPEN')
DB has some data initially few tuples have 'UNDER-CONSTRUCTION' status.
Here is my solution:
Custom Type for enum mapping:
public class PsqlEnum extends EnumType {
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
final SharedSessionContractImplementor session) throws SQLException {
if(value == null) {
st.setNull( index, Types.OTHER );
}
else {
st.setObject(
index,
value.toString(),
Types.OTHER
);
}
}
}
Entity for ORM(hibernate) model :
@Entity
@Table(name = "place")
@TypeDef(
name = "pgsql_enum",
typeClass = PsqlEnum.class
)
public class Place implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id",nullable = false)
@Getter @Setter private Integer id;
@Column(name = "name")
@Getter @Setter private String name;
@Getter
@Setter
@Enumerated(EnumType.STRING)
@Type(type = "pgsql_enum")
@Column(name = "status_level",columnDefinition = "status_enum")
private STATUS status;
}
Enum Class :
public enum STATUS{
CLOSED("CLOSED"),
UNDERCONSTRUCTION("UNDER-CONSTRUCTION"),
OPEN("OPEN");
private final String label;
Exposure(String label){
this.label = label;
}
public String getLabel() {
return label;
}
public static STATUS fromLabel(String label){
for(STATUS currentEnum:STATUS.values()){
if(currentEnum.getLabel().equals(label)){
return currentEnum;
}
}
return null;
}
@Override
public String toString() {
return this.label;
}
}
It works fine on insertion operation while on retrieving data it throws an error : java.lang.IllegalArgumentException: Unknown name value [UNDER-CONSTRUCTION] for enum class
I tried every given reference solution in StackOverflow no one is working fine for my problem.
Hibernate uses function name()
of Enum
when it serialize value of enumerated field and Enum.valueOf(...)
when it is deserialize entity.
Thats why you cant use "UNDER-CONSTRUCTION"
in values
So I am able to figure out what I was missing. Basically I have to override both nullSafeGet and nullSafeSet in PsqlEnum.java file.
I reached to solution by following this java file .
So Either define custom UserType like defined in link or custom EnumType both worked for me.
In above code PsqlEnum.java in custom EnumType I was missing overriding nullSafeGet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.