Bourne
Bourne

Reputation: 1927

Avoiding "columnDefinition" attribute in @Column annotation in hibernate

Is there a way to avoid the columnDefinition attribute of @Column annotation in hibernate?
Suppose I have a column in a table in the DB which is defined as enum('US', 'IN', 'GB', 'DE'etc) i.e. enum of all country codes . I map this to a attribute 'country_code' in java class representing the table using hibernate annotations. But without defining the columnDefinition hibernate throws an Exception saying
org.hibernate.HibernateException: Wrong column type for country_code. Found enum, expected varchar.

Is there a way to avoid columnDefinition attribute in @Column annotation?
Eg:

private UserType userType;
@Column(name="user_type")
@Enumerated(EnumType.STRING)
public UserType getUserType() {
    return userType;
}

public void setUserType(UserType userType) {
    this.userType = userType;
}

When I run the app with this mapping it throws an Exception saying:

org.hibernate.HibernateException: Wrong column type in gobe_user_db.gobe_User for column user_type. Found: enum, expected: varchar(255)

However when I add the "columnDefinition="enum('abc', 'xyz')" to the @Column attribute it works.

Upvotes: 0

Views: 4559

Answers (2)

Dileep
Dileep

Reputation: 5440

The code you specified has a missing definition, hope the this will resolve the issue.

   public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}

   public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}

   @Entity public class Employee {
       public EmployeeStatus getStatus() {...}
       ...
       @Enumerated(STRING)
       public SalaryRate getPayScale() {...}
       ...
   }

Look here for examples

Upvotes: 1

SAP
SAP

Reputation: 468

You have missed @Enumerated(EnumType.STRING), use it with @Column

Upvotes: 0

Related Questions