使用@ManyToMany字段的Spring Data DTO投影

时间:2018-05-14 15:07:09

标签: java spring kotlin spring-data spring-data-jpa

我的User实体包含此字段:

@ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "FAVORITE_TAGS", joinColumns = arrayOf(JoinColumn(name = "USER_ID")), inverseJoinColumns = arrayOf(JoinColumn(name = "TAG_ID")))
    @BatchSize(size = 24)
    val favoriteTags: MutableSet<Tag>? = null

我定义了以下DTO:

data class UserProfileDto(
        ...
        val favoriteTags: MutableSet<Tag>?,
        ...
): UserDto()

最后,这是UserRepository方法之一(DTO动态投影):

fun <T: UserDto> findByUsername(username: String, type: Class<T>): T?

问题在于,当我呼叫userRepository.findByUsername("username", UserProfileDto::class.java)时,我得到以下异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.cillariodelsanto.socialnotes.entity.dto.UserProfileDto]. Expected arguments are: 
    java.lang.String, java.lang.String, java.lang.String, double, long, com.cillariodelsanto.socialnotes.entity.Tag, long, long, long, long [select new com.cillariodelsanto.socialnotes.entity.dto.UserProfileDto(generatedAlias0.username, generatedAlias0.name, generatedAlias0.userPicPath, generatedAlias0.averageScore, generatedAlias0.documentsRatingsCount, favoriteTags, generatedAlias0.documentsCount, generatedAlias0.favoriteDocumentsCount, generatedAlias0.followingCount, generatedAlias0.followersCount) from com.cillariodelsanto.socialnotes.entity.User as generatedAlias0 left join generatedAlias0.favoriteTags as favoriteTags where generatedAlias0.username=:param0]

问题在于MutableSet(我已尝试使用CollectionSet,但结果是相同的)。有办法解决吗?

我知道使用接口而不是DTO可以解决问题,但我想知道是否可以使用DTO实现相同的结果。非常感谢。

0 个答案:

没有答案