JPA 2.1中带有枚举的@ConstructorResult [英] @ConstructorResult with Enum in JPA 2.1
问题描述
我不知道如何在使用@SqlResultSetMapping的@ConstructorResult时在@ColumnResult类型中使用Enum
I am not having any idea how to use Enum in @ColumnResult Type while using @ConstructorResult of @SqlResultSetMapping
@SqlResultSetMapping(name="DetailAndResult",
classes={
@ConstructorResult(targetClass=DetailAndResult.class, columns={
@ColumnResult(name="id", type= String.class),
@ColumnResult(name="runId", type=Integer.class),
@ColumnResult(name="subRunId", type=Integer.class),
@ColumnResult(name="transactionId", type=Integer.class),
@ColumnResult(name="referenceNumber", type=String.class),
@ColumnResult(name="customerName", type=String.class),
@ColumnResult(name="transactionType", type=TransactionType.class),
@ColumnResult(name="transactionResultStatus", type=String.class)
})
}
)
在上述配置中,名称"transactionType"的名称为TransactionType枚举.在这里使用Enum的正确方法是什么.
in above configuration, name 'transactionType' is of TransactionType Enum. What is the correct way to use Enum here.
如果以上是正确的方法,那么我将收到此异常(如果我将删除Enum字段,则没有异常),因此认为应该有另一种使用此方法的方法.
if above is the correct way then I am getting this exception (If I will remove the Enum field then there is no exception) so thinking that there should be another way to use this.
Caused by: javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_51]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:333) ~[spring-orm-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at com.sun.proxy.$Proxy146.getResultList(Unknown Source) ~[na:na]
使用hibernateTemplate,我们使用的是sqlquery.addscalar,并且有一种方法可以使用org.hibernate.type.Type和
With hibernateTemplate, we were using sqlquery.addscalar and there was a way to use Enum there using org.hibernate.type.Type and
TypeLocatorImpl(new TypeResolver()).custom(EnumType.class, params)
请建议是否将类似的东西用于@SqlResultSetMapping和@ConstructorResult
Please suggest if something like this will be used for @SqlResultSetMapping and @ConstructorResult
推荐答案
我也遇到了这个问题,经过相当广泛的搜索后没有发现任何东西.我已经深入研究了源代码,并且据我所知,根本没有任何类型的枚举处理(尽管我当然会遗漏一些东西).
I've also run into this problem and haven't found anything after a fairly extensive search. I've gone as far as looking at the source code, and as far as I could tell there wasn't any kind of handling for enums at all (although of course I could be missing something).
我最终要做的是创建一个替代构造函数,该构造函数接受枚举类型的String,然后将其传递给枚举的valueOf()方法.
What I ended up doing was creating an alternative constructor that took in a String for the enum type, and then passed it into the enum's valueOf() method.
例如
更改您的@SqlResultSetMapping以执行以下操作:
change your @SqlResultSetMapping to do this:
@ColumnResult(name="transactionType", type=String.class),
然后在您的类的构造函数中:
And then in the constructor of your class:
public DetailAndResult(..., String transactionType, ...) {
...
this.transactionType = TransactionType.valueOf(transactionType);
...
}
我们必须这样做很烦人,但是只要您的枚举以字符串形式存储在db中(即,您实体上的列用@Enumerated(EnumType.STRING)注释),它就可以工作.
It's annoying that we have to do this, but so long as your enum is stored as a String in the db (i.e. the column on your entity is annotated with @Enumerated(EnumType.STRING)), it works.
这篇关于JPA 2.1中带有枚举的@ConstructorResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!