我有一个枚举类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
转换后,hibernate抛出以下异常:
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
,它可以正常工作,但我需要这是一个可变类型。我不明白字段的可变性是如何在休眠中创建问题的。
注意:我正在使用Kotlin 1.1.2-2&amp; Hibernate 5.2版本。
答案 0 :(得分:32)
您是否尝试过更改
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>
。