@ConstructorResult with Enum in JPA 2.1

偶尔善良 提交于 2019-12-04 04:27:00

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).

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.

E.g.

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);
    ...
}

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.

Came across a undocument solution for this one. In the type of the @ColumnResult you can place a hibernate UserType that maps the type in type the constructor is expecting. This works for hibernate and i am unsure if other JPA implementations would support this.

So in you example you would implement a custom UserType for enums and put that class in the @ColumnResult.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!