9

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)

                })
        }
)

in above configuration, name 'transactionType' is of TransactionType Enum. What is the correct way to use Enum here.

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]

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)

Please suggest if something like this will be used for @SqlResultSetMapping and @ConstructorResult

4

3 回答 3

12

我也遇到了这个问题,经过相当广泛的搜索后没有找到任何东西。我已经查看了源代码,据我所知,根本没有任何类型的枚举处理(当然我可能会遗漏一些东西)。

我最终做的是创建一个替代构造函数,它接受枚举类型的字符串,然后将其传递给枚举的 valueOf() 方法。

例如

更改您的 @SqlResultSetMapping 以执行此操作:

@ColumnResult(name="transactionType", type=String.class),

然后在你的类的构造函数中:

public DetailAndResult(..., String transactionType, ...) {
    ...
    this.transactionType = TransactionType.valueOf(transactionType);
    ...
}

我们必须这样做很烦人,但只要您的枚举作为字符串存储在数据库中(即您的实体上的列使用@Enumerated(EnumType.STRING) 进行注释),它就可以工作。

于 2015-10-29T12:29:07.353 回答
1

遇到了一个未记录的解决方案。在类型中,@ColumnResult 您可以放置​​一个休眠用户类型,它映射构造函数期望的类型中的类型。这适用于休眠,我不确定其他 JPA 实现是否支持这一点。

因此,在您的示例中,您将为枚举实现自定义 UserType 并将该类放在@ColumnResult.

于 2014-12-03T08:17:43.440 回答
0

您仍然可以在实体字段上使用 @Enumerated(EnumType.STRING),

我的 @SqlResultSetMapping 看起来有点不同:

@FieldResult(name="state", column="STATE"),

于 2021-02-09T05:22:51.950 回答