Kotlin:Collection既没有genericstypes,也没有OneToMany.targetEntity()
我有一个枚举类RoleType
public enum RoleType { SYSTEM_ADMIN, PROJECT_ADMIN, USER; }
在我的User
实体类,我有以下映射的枚举集合。 这是Java
代码:
@JsonProperty @ElementCollection @Enumerated(EnumType.STRING) @CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id")) private Set roles;
我把这个User
实体类转换成Kotlin
,这里是代码:
@JsonProperty @Enumerated(EnumType.STRING) @ElementCollection @CollectionTable(name = "user_role", joinColumns = arrayOf(JoinColumn(name = "user_id"))) var roles: kotlin.collections.Set? = null
转换后,hibernate抛出以下exception:
Collection has neither generic type or OneToMany.targetEntity() defined: com.abmodel.User.roles
在Java之前它工作正常。
我也尝试像这样在@ElementCollection
添加targetClass
:
@ElementCollection(targetClass = RoleType::class)
但它也抛出了另一个例外。
Fail to process type argument in a generic declaration. Member : com.abmodel.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.abmodel.User#roles Type: class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
注意 :如果我将roles
的修饰符从var
更改为val
,则表示正在工作,但是我需要这是一个可变types。 我不明白一个字段的可变性是如何在hibernate中创建问题的。
注意 :我正在使用Kotlin 1.1.2-2和Hibernate 5.2版本。
你尝试过改变吗?
var roles: Set? = null
至
var roles: MutableSet? = null
如果你看看Set
的接口定义,你会看到它定义为public interface Set
而MutableSet
定义为public interface MutableSet
Set
的Java相当于我相信是Set extends E>
Set extends E>
而不是你正在寻找Set
。