【问题标题】:Hibernate does not work with enums with @Id and @Convert or @Converter(autoApply=true)Hibernate 不适用于带有 @Id 和 @Convert 或 @Converter(autoApply=true) 的枚举
【发布时间】:2016-01-06 11:52:30
【问题描述】:

一些

public class SomeDao {
  @Id private MyEnum id;

  public MyEnum getId() { return id; }
}

@Converter( autoApply=true )
public MyEnumConv { ... }

你会得到类似于 (JPA 2.1/Hibernate 4.3.7) 的东西:

org.postgresql.util.PSQLException: Unzulässiger Wert für den Typ int : enumDbStrX.
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toInt(AbstractJdbc2ResultSet.java:2955) ~[postgresql-9.3-1102-jdbc41.jar:na]
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2138) ~[postgresql-9.3-1102-jdbc41.jar:na]
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2589) ~[postgresql-9.3-1102-jdbc41.jar:na]
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.getInt(NewProxyResultSet.java:2426) ~[c3p0-0.9.2.1.jar:0.9.2.1]
    at org.hibernate.type.EnumType$OrdinalEnumValueMapper.getValue(EnumType.java:372) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:107) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:785) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:721) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:953) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:921) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2554) ~[hibernate-core-4.3.7.Final.jar:4.3.7.Final]

【问题讨论】:

    标签: hibernate enums jpa-2.1


    【解决方案1】:

    对我来说,使用一些 dummy @Id String id_ 字段(这是 Hibernate 或 JPA 要求的,有时/经常(取决于您的场景/映射)在逻辑上/与模型无关)工作得很好:

    public class SomeDao {
    
      //@Id @Convert( converter=MyEnumConv.class )  // explicitely providing converter 
                                                    // would not help
      @Column( insertable=false, updatable=false)   // necessary because of conflict 
                                                    // with id_ dummy field
      private MyEnum id;
    
      /** @deprecated use {@link #id} instead ; workaround for missing @Id-enum
        *                                       mapping in Hibernate 
        */
      private @Deprecated @Id @Column(name="id") String id_;
    
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多