在hibernate中避免@Column注释中的“columnDefinition”属性

时间:2013-10-14 11:24:08

标签: java mysql hibernate hibernate-mapping

有没有办法在hibernate中避免@Column注释的columnDefinition属性?
假设我在DB中的表中有一列,定义为枚举('US','IN','GB','DE'etc),即所有国家代码的枚举。我将它映射到java类中的属性'country_code',表示使用hibernate注释的表。但是如果没有定义columnDefinition,hibernate会抛出一个例外说明 org.hibernate.HibernateException:country_code的列类型错误。找到enum,期望varchar。

有没有办法避免@Column注释中的columnDefinition属性?
例如:

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

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

当我使用此映射运行应用程序时,它会抛出一个Exception:

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

然而,当我将“columnDefinition =”enum('abc','xyz')“添加到@Column属性时,它可以工作。

2 个答案:

答案 0 :(得分:0)

您错过了@Enumerated(EnumType.STRING),将其与@Column

一起使用

答案 1 :(得分:0)

您指定的代码缺少定义,希望这样可以解决问题。

   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() {...}
       ...
   }

查看here示例

相关问题