Hibernate 不适用于带有@Id 和@Convert 或@Converter(autoApply=true) 的枚举
Hibernate does not work with enums with @Id and @Convert or @Converter(autoApply=true)
有一些
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]
对我来说,使用一些 虚拟 @Id String id_
字段(这是 Hibernate 或 JPA 和 sometimes/often 所必需的(取决于您的 scenarios/mappings)不需要 logically/model-related)工作正常:
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_;
...
}
有一些
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]
对我来说,使用一些 虚拟 @Id String id_
字段(这是 Hibernate 或 JPA 和 sometimes/often 所必需的(取决于您的 scenarios/mappings)不需要 logically/model-related)工作正常:
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_;
...
}