Question

I am using Hibernate custom user type to map enum to a table varchar/char. I am using custom user type code from here (https://community.jboss.org/wiki/Java5StringValuedEnumUserType). I am trying following annotation to do the mapping but its not working.

@Transient
@Type(type = "data.model.base.StringValuedEnumType", parameters = {
        @Parameter(name = "enumClass", value = "data.common.TypeEnum"),
        @Parameter(name = "identifierMethod", value = "dbCode") })
private TypeEnum typeEnum;

TypeEnum code:

public enum TypeEnum implements StringValuedEnum {

OFF("OFF", "O"),
ON("ON, "O"),

private String dbCode;
private String desc;

TypeEnum(String desc, String dbCode) {
    this.desc=desc;
    this.dbCode = dbCode;
}

@Override
public String dbCode() {
    return dbCode;
}

public String desc() {
    return desc;
}
}

I believe I am doing something wrong in the annotation but I am not able to figure out what is it. Any idea anyone?

Was it helpful?

Solution

I have found it. I updated the annotation by removing @Transient and adding in a @Column for the mapping. I also updated the code to take care of the passed name and removed defaultValue.

@Column(name = "TYP_CD", length = 1)
@Type(type = "data.model.base.StringValuedEnumType", parameters = {
    @Parameter(name = "enumClass", value = "data.common.TypeEnum")})
private TypeEnum typeEnum;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top