web-dev-qa-db-ja.com

Kotlin:コレクションにジェネリック型もOneToMany.targetEntity()もありません

EnumクラスRoleTypeがあります

public enum RoleType {
    SYSTEM_ADMIN, PROJECT_ADMIN, USER;
}

私のUserエンティティクラスには、enumコレクションの次のマッピングがあります。これは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で問題なく動作していました。

次のように、@ElementCollectiontargetClassを追加してみました。

@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に変更すると機能しますが、これは変更可能な型にする必要があります。フィールドのmutabilityが休止状態でどのように問題を引き起こしているのか理解できません。

:Kotlin 1.1.2-2およびHibernate 5.2バージョンを使用しています。

21
Sriram

変えてみましたか

var roles: Set<RoleType>? = null

var roles: MutableSet<RoleType>? = null

Setのインターフェース定義を見ると、public interface Set<out E> : Collection<E>として定義されているのに対し、MutableSetpublic interface MutableSet<E> : Set<E>, MutableCollection<E>として定義されていることがわかります。

Set<out E>のJava同等のものだと思いますSet<? extends E>は、探していたものではなくSet<E>です。

37