科特林:收集既没有泛型类型或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<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

:如果我的修改改变rolesvarval,它的工作,但我需要这是一个可变的类型。我不明白是怎么可变性的字段是Hibernate产生的问题。

:我使用科特林1.1.2-2和Hibernate 5.2版本。

扬弗拉基米尔Mostert:

你试图改变

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章