C.Norris
C.Norris

Reputation: 97

Convert enum to char

I'm working on a Hibernate project, here is an extract of my class:

@Entity
@Table(name = "T_JOB")
@SequenceGenerator(name = "seqJob", sequenceName = "SEQ_JOB", allocationSize = 1)
@Getter
@Setter
public class Job {

    @Convert(converter = HibernateBooleenVersCaractere.class)
    @Column(name = "STATUT_EXTRACTION")
    private boolean statutExtraction;
}

In my database STATUT_EXTRACTION is a CHAR type that's why I used a converter (HibernateBooleenVersCaractere implements AttributeConverter) and it worked perfectly. The boolean statutExtraction was usefull until now because I had only two state (OK = true = 1 in DB, Error = false = 0 in DB). But now I would like to add a state. In order to do that I created a enum state and used enumerated type:

@Entity
@Table(name = "T_JOB")
@SequenceGenerator(name = "seqJob", sequenceName = "SEQ_JOB", allocationSize = 1)
@Getter
@Setter
public class Job {
    public enum Statut {
        ERROR, OK, LOAD
    }

    @Column(name = "STATUT_EXTRACTION")
    @Enumerated(EnumType.STRING)
    private Statut statutExtraction;
}

Here I got an error caused by EnumType different from STATUT_EXTRACTION type in database I guess.

I had a second idea: use a converter as I did for converting boolean to char:

public class HibernateEnumVersChar
        implements
        AttributeConverter<Statut, Character> {

    @Override
    public Character convertToDatabaseColumn(final Statut statut) {
        if (statut == Statut.OK) {
            return '1';
        } else if (statut == Statut.LOAD) {
            return '2';
        } else if (statut == Statut.ERROR) {
            return '0';
        } else {
            return null;
        }

    }

    @Override
    public Statut convertToEntityAttribute(final Character dbData) {
        if (dbData == '1') {
            return Statut.OK;
        } else if (dbData == '2') {
            return Statut.LOAD;
        } else if (dbData == '0') {
            return Statut.ERROR;
        } else {
            return null;
        }
    }

}

And changed my Job class to:

@Entity
@Table(name = "T_JOB")
@SequenceGenerator(name = "seqJob", sequenceName = "SEQ_JOB", allocationSize = 1)
@Getter
@Setter
public class Job {
    public enum Statut {
        ERROR, OK, LOAD
    }

    @Convert(converter = HibernateEnumVersChar.class)
    @Column(name = "STATUT_EXTRACTION")
    private Statut statutExtraction;
}

And here I have this error:

Error attempting to apply AttributeConverter

Sadly I can't change the database type (char) because I have a lot of data.

Has some one an idea where can my error be (or why not a different way to do what I need to do !) ?

Upvotes: 1

Views: 1372

Answers (1)

cнŝdk
cнŝdk

Reputation: 32145

What I suggest here is to use the @Enumerated annotation with the attribute EnumType.ORDINAL, ORDINAL here will get you a number 0, 1 or 2 giving the order the value was defined in the Enum.

So when you define:

public enum Statut {
    ERROR, OK, LOAD
}

So here we will have:

Statut.ERROR.ordinal()  ---> gives 0
Statut.OK.ordinal()  ---> gives 1
Statut.LOAD.ordinal()  ---> gives 2

Conculsion:

Change your column definition like this:

@Enumerated(EnumType.ORDINAL)
@Column(name = "STATUT_EXTRACTION")
private Statut statutExtraction;

Note:

The database column should be changed to NUMBER(1,0), or you can convert it to a Char inside your Enum definition, please read Hibernate & Enum handling article for further details.

Upvotes: 1

Related Questions