Sri*_*ram 16 java enums hibernate jpa kotlin
我有一个Enum课程 RoleType
public enum RoleType {
SYSTEM_ADMIN, PROJECT_ADMIN, USER;
}
Run Code Online (Sandbox Code Playgroud)
在我的User实体类中,我为枚举集合提供了以下映射.这是Java代码:
@JsonProperty
@ElementCollection
@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
private Set<RoleType> roles;
Run Code Online (Sandbox Code Playgroud)
我将此User实体类转换Kotlin为以下代码:
@JsonProperty
@Enumerated(EnumType.STRING)
@ElementCollection
@CollectionTable(name = "user_role", joinColumns = arrayOf(JoinColumn(name = "user_id")))
var roles: kotlin.collections.Set<RoleType>? = null
Run Code Online (Sandbox Code Playgroud)
转换后,hibernate抛出以下异常:
Collection has neither generic type or OneToMany.targetEntity() defined: com.a.b.model.User.roles
Run Code Online (Sandbox Code Playgroud)
它在Java之前运行良好.
我也试过像这样添加targetClassin @ElementCollection:
@ElementCollection(targetClass = RoleType::class)
Run Code Online (Sandbox Code Playgroud)
但它也引发了另一个例外.
Fail to process type argument in a generic declaration. Member : com.a.b.model.User#roles Type: class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
ERROR [2017-05-27 04:46:33,123] org.hibernate.annotations.common.AssertionFailure: HCANN000002: An assertion failure occurred (this may indicate a bug in Hibernate)
! org.hibernate.annotations.common.AssertionFailure: Fail to process type argument in a generic declaration. Member : com.a.b.model.User#roles Type: class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
Run Code Online (Sandbox Code Playgroud)
注:如果我改变的修改roles,从var到val,它的工作,但我需要这是一个可变的类型.我不明白一个字段的可变性是如何在hibernate中创建问题的.
注意:我正在使用Kotlin 1.1.2-2和Hibernate 5.2版本.
Jan*_*ert 39
你试过改变吗?
var roles: Set<RoleType>? = null
Run Code Online (Sandbox Code Playgroud)
至
var roles: MutableSet<RoleType>? = null
Run Code Online (Sandbox Code Playgroud)
如果您查看接口定义Set,您将看到它定义为,public interface Set<out E> : Collection<E>而MutableSet定义为public interface MutableSet<E> : Set<E>, MutableCollection<E>
Set<out E>相信Java的等价物Set<? extends E>而不是你想要的东西Set<E>.
| 归档时间: |
|
| 查看次数: |
3152 次 |
| 最近记录: |