我有一个枚举类 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<RoleType> roles;
我转换这个User
实体类Kotlin
,这里是代码:
@JsonProperty
@Enumerated(EnumType.STRING)
@ElementCollection
@CollectionTable(name = "user_role", joinColumns = arrayOf(JoinColumn(name = "user_id")))
var roles: kotlin.collections.Set<RoleType>? = null
转换后,休眠抛出以下异常:
Collection has neither generic type or OneToMany.targetEntity() defined: com.a.b.model.User.roles
它在Java的前工作正常。
我也尝试添加了targetClass
在@ElementCollection
这样的:
@ElementCollection(targetClass = RoleType::class)
但它也抛出另一个异常。
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
注:如果我的修改改变roles
从var
到val
,它的工作,但我需要这是一个可变的类型。我不明白是怎么可变性的字段是Hibernate产生的问题。
注:我使用科特林1.1.2-2和Hibernate 5.2版本。
你试图改变
var roles: Set<RoleType>? = null
至
var roles: MutableSet<RoleType>? = null
如果你看一下的接口定义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>
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句