gpt4 book ai didi

hibernate - 参数值 [....] 与预期类型不匹配 [java.util.Collection (n/a)]

转载 作者:行者123 更新时间:2023-12-05 06:25:59 25 4
gpt4 key购买 nike

我正在使用 spring-boot , spring-data-JPAHibernate我有一个 Form Entity和一个 Group Entity他们有一个 @ManyToMany它们之间的关系。

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "form_group",
joinColumns = @JoinColumn( name = "form_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"))
@JsonIgnore
private Collection<Group> groups;

FormRepository类我有一个方法叫 public List<Form> findByGroups(Collection<Group> userGroups);它采用 Collection<Group> 类型的参数并期望返回属于至少一个作为方法参数传递的组的所有表单。这是查询:

@Query("SELECT new com.nsia.model.Form(f.id, f.name, f.description, f.createdAt, f.groups, COUNT(i.id)) from Form f LEFT JOIN f.instances i WHERE f.groups IN (?1) group by f.id")

如您所见userGroups类型为 Collection<Group>Form Entity 里面它是同一类型。当方法findByGroup被调用,它抛出 java.lang.IllegalArgumentException这是完整的信息:

java.lang.IllegalArgumentException: Parameter value [Group {id=4, name='DATA_ENTRY_GROUP', description='DATA ENTRY GROUP'}] did not match expected type [java.util.Collection (n/a)]

我确定 userGroups类型为 Collection<Group>因为这就是我如何将它放入 FormServiceImpl 中类:

        Collection<Group> groups = userService.getLoggedInUser().getGroups();
formsList = formRepository.findByGroups(groups);

StackOverflow 中有很多类似的问题,我已经尝试了每一个问题,但没有一个对我有用,我们将不胜感激。谢谢

最佳答案

你正在尝试的是不可能的。

构造函数表达式不能以集合作为参数,因为底层SQL select语句的结果总是一个表。

所以你唯一能做的就是像这样在你加入 f.groups 的地方得到一个笛卡尔积。

@Query("SELECT new com.nsia.model.Form(f.id, f.name, f.description, f.createdAt, g, COUNT(i.id)) from Form f LEFT JOIN f.instances JOIN f.groups g WHERE f.groups IN (?1) group by f.id")

因此您将获得每个组的记录,但这可能不是您想要的。

关于hibernate - 参数值 [....] 与预期类型不匹配 [java.util.Collection (n/a)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com