Kotlin:Collection既没有泛型类型也没有OneToMany.targetEntity() [英] Kotlin: Collection has neither generic type or OneToMany.targetEntity()

查看:146
本文介绍了Kotlin:Collection既没有泛型类型也没有OneToMany.targetEntity()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Enum类 RoleType

public enum RoleType {
    SYSTEM_ADMIN, PROJECT_ADMIN, USER;
}

在我的 User 实体类,下面是枚举集合的映射。这是 Java 代码:

In my User entity class, I've the following mapping for the enum collection. This is the Java code:

@JsonProperty
@ElementCollection
@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
private Set<RoleType> roles;

我将此 User 实体类转换为 Kotlin ,下面是代码:

I converted this User entity class to Kotlin and here is the code:

@JsonProperty
@Enumerated(EnumType.STRING)
@ElementCollection
@CollectionTable(name = "user_role", joinColumns = arrayOf(JoinColumn(name = "user_id")))
var roles: kotlin.collections.Set<RoleType>? = null

转换后,hibernate抛出以下异常:

After the conversion, hibernate is throwing the following exception:

Collection has neither generic type or OneToMany.targetEntity() defined: com.a.b.model.User.roles

以前在Java中运行良好。

It was working fine before in Java.

我还尝试添加 @ElementCollection 中的targetClass 像这样:

I also tried adding the targetClass in @ElementCollection like this:

@ElementCollection(targetClass = RoleType::class)

,但它还会引发另一个异常。

but it's also throwing another exception.

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

注意:如果我将角色的修饰符从 var 更改为 val ,它可以正常工作,但是我需要将其设置为可变类型。我不了解字段的可变性是如何在休眠状态下造成问题的。

Note: If i change the modifier of roles from var to val, it's working, but i need this to be a mutable type. I don't understand how the mutability of a field is creating issues in hibernate.

注意:我m使用Kotlin 1.1.2-2& Hibernate 5.2版本。

Note: I'm using Kotlin 1.1.2-2 & Hibernate 5.2 version.

推荐答案

您是否尝试过更改

var roles: Set<RoleType>? = null

var roles: MutableSet<RoleType>? = null

如果您看一下 Set的接口定义,您将看到它定义为公共接口Set< out E>。 :Collection< E> ,而 MutableSet 被定义为公共接口MutableSet< E>。 :Set< E>,MutableCollection< E>

If you have a look at the interface definition of Set, you'll see it's defined as public interface Set<out E> : Collection<E> whereas MutableSet is defined as public interface MutableSet<E> : Set<E>, MutableCollection<E>

Set< out E> 我相信是 Set<吗?扩展了E> 而不是您要查找的 Set< E>

Set<out E> 's Java equivalent I believe is Set<? extends E> instead of what you were looking for Set<E>.

这篇关于Kotlin:Collection既没有泛型类型也没有OneToMany.targetEntity()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆