I have enum ClientType{INTERNAL,ADMIN}, i am able to persist enum with hibernate annotations. but inserted value is INTERNAL,ADMIN. How can i define my own vlaue. I want table to contain "I" for INTERNAL. How can i do this hibernate annotations.
A:
There is no support for this built into Hibernate/JPA that I'm aware of. The best way to do this is to have a char
property in your entity containing either 'I' or 'A', but to only expose the ClientType
enum:
public enum ClientType() {
INTERNAL('I'), ADMIN('A');
private final char dbValue;
private ClientType(char dbValue) {
this.dbValue = dbValue;
}
public static ClientType findByDbValue(char dbValue) {
for (ClientType t : ClientType.values()) {
if (t.dbValue == dbValue) {
return t;
}
}
throw new IllegalArgumentException ("Unknown type " + dbValue);
}
}
@Column
private char clientType;
public ClientType getClientType() {
return ClientType.findByDbValue(this.clientValue);
}
public void setClientType(ClientType type) {
this.clientType = type.dbValue;
}
Henning
2010-09-17 11:13:18