10

I have a database table using an enum. This is already working with hibernate (using XML), and I'm trying to convert it to annotations as this is one of the last pieces to still be using xml notation.

The column definition:

enum('Active','Pending','Cancelled','Suspend')

The following works:

<property
    name="status"
    column="STATUS"
    type="string"
    not-null="true" />

This does not work:

@Column(name = "status")
public String status;

The annotation-style leads to the following exception upon startup: org.hibernate.HibernateException: Wrong column type in UserDTO for column status. Found: enum, expected: varchar(255)

Is there any way for me to force this to accept a String as it did using the XML notation?

1 Answer 1

20

I figured it out. It should be:

@Column(name="status", columnDefinition="enum('Active','Pending','Cancelled','Suspend')")
public String status;
Sign up to request clarification or add additional context in comments.

2 Comments

your solution is correct at least for MySQL database, but fails for H2 databases used in tests for instance (case for automatic schema creation). It seems that is nor 100% portable, as far as I remember there are problems on PostgresSQL as well (at least if we expect that schema'd auto generated).
It works for me, too. I had an additional annotation @Enumerated(EnumType.STRING) but it resulted in an error was annotated as enumerated, but its java type is not an enum [java.lang.String]

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.