4

I have integer column as "status" in my db.

My enum class:

public enum MemberStatus {
   PASSIVE(0),ACTIVE(1);

   private int value;

   private MemberStatus(int value) {
      this.value = value;
   }

   public int getValue() {
      return value;
   }

}

My entity field:

@Column(name = "status", nullable = false)
@Enumerated(EnumType.ORDINAL)
private MemberStatus status;

Hibernate Log:

org.postgresql.util.PSQLException: ERROR: column "status" is of type integer but expression is of type bytea. Hint: You will need to rewrite or cast the expression.bytea

I use PostgreSQL. How to solve this problem? Any idea?

4
  • Are you using org.hibernate.dialect.PostgreSQLDialect? Commented May 12, 2015 at 14:57
  • I use it : hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect Commented May 12, 2015 at 15:01
  • If you have control over the database (a client won't need to deploy on MySQL), then you can use string representation for the enum and use a Postgres enum type for the column, and everything will Just Work, with the advantages of a DB-native enum. Commented May 12, 2015 at 15:42
  • @Gyhot have you tried anything yet? Commented May 14, 2015 at 8:25

1 Answer 1

4

I suggest you use a converter.

It's the cleanest solution i came to because:

  • you no longer have an issue with the order in which you add values to the enum or if you refactor the enum elements name
  • you have more flexibility on what database type your column has

You can define the field as:

@Column(name = "status", nullable = false)
@Convert(converter = MemberStatusEnumConverter.class)
private MemberStatus status;

The enum becomes simpler:

public enum MemberStatus {
   PASSIVE,
   ACTIVE;
}

And your converter class MemberStatusEnumConverter:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class MemberStatusEnumConverter implements    
AttributeConverter<MemberStatus,Integer>{
    @Override
    public Integer convertToDatabaseColumn(MemberStatus attribute) {
        switch (attribute) {
            case PASSIVE:
                return new Integer(0);
            case COUNTYLEVEL:
                return new Integer(1);
            default:
                throw new IllegalArgumentException("Unknown" + attribute);
        }
    }

    @Override
    public MemberStatus convertToEntityAttribute(Integer dbData) {
        if (dbData == 0){
            return MemberStatus.PASSIVE;
        } else if (dbData == 1){
            return MemberStatus.ACTIVE;
        }
        else{
            throw new IllegalArgumentException("Unknown" + dbData);
        }
    }
}

This article describes the solution i implemented for your example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.