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.
Asked
Active
Viewed 1,144 times
0
-
possible duplicate of [Map enum in JPA with fixed values ?](http://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values) – Pascal Thivent Sep 15 '10 at 08:12
-
Thanks for above link. It is very helpful – dmay Oct 06 '10 at 05:43
1 Answers
0
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
- 16,063
- 3
- 51
- 65