为什么Spring Data JPA + Hibernate生成不正确的SQL? [英] Why does Spring Data JPA + Hibernate generate incorrect SQL?

查看:714
本文介绍了为什么Spring Data JPA + Hibernate生成不正确的SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有一对多关系的实体。我想获得与其他实体集合相关的所有实体
。这是我的课程:

 公共类实例{

@Id
@GeneratedValue
私人长ID;

@OneToMany(mappedBy =instance)
private Set< Action> actions = new HashSet<>();

}

公共类动作{

@Id
@GeneratedValue
私人长ID;

@ManyToOne
@JoinColumn(name =instance_id)
私有实例实例;

}

另外我有以下资源库:

  public interface InstanceRepository扩展了JpaRepository< Instance,Long> {

列表< Instance> findByActions(Set< Action> actions);


当我用空或单元素集调用方法时,我没有得到任何错误。但是如果该集合包含更多元素
,我会得到一个异常。 MySQL表示操作数应该包含1列(s)。为空或单元素
集生成的SQL是

  select instance0_.id as id1_3_ 
from instance instance0_
在instance0_.id = actions1_.instance_id
其中actions1_.id =?左边的外部连接操作actions1_

和其他集合

  select instance0_.id as id1_3_ 
from instance instance0_
left outer join action actions1_
on instance0_.id = actions1_.instance_id
where actions1_.id = (?,?,?,...)

这显然是错误的,它应该是

  select instance0_.id as id1_3_ 
from instance instance0_
left outer join action actions1_
on instance0_.id = actions1_.instance_id
where(?,?,?,...)中的actions1_.id

为什么Hibernate会生成这个SQL,我该如何解决它?为什么Hibernate会生成这个SQL,我该如何解决它?

规格您必须将此方法定义为:

  List< Instance> findByActionsIn(Collection< Action> actions); 


I have two entities with a one-to-many relationship. I want to get all entities that are tied to a set of the other entity. This are my classes:

public class Instance {

    @Id
    @GeneratedValue
    private long id;

    @OneToMany(mappedBy = "instance")
    private Set<Action> actions = new HashSet<>();

}

public class Action {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne
    @JoinColumn(name = "instance_id")
    private Instance instance;

}

Furthermore I have the following repository:

public interface InstanceRepository extends JpaRepository<Instance, Long> {

    List<Instance> findByActions(Set<Action> actions);

}

When I call the method with empty or single element sets, I get no errors. But if the set contains more elements I get an exception. MySQL says Operand should contain 1 column(s). The generated SQL for empty or single element sets is

select instance0_.id as id1_3_
from instance instance0_
left outer join action actions1_
on instance0_.id=actions1_.instance_id
where actions1_.id=?

and for other sets

select instance0_.id as id1_3_
from instance instance0_
left outer join action actions1_
on instance0_.id=actions1_.instance_id
where actions1_.id=(?, ?, ?, ...)

This is obviously wrong and it should be something like

select instance0_.id as id1_3_
from instance instance0_
left outer join action actions1_
on instance0_.id=actions1_.instance_id
where actions1_.id in (?, ?, ?, ...)

Why does Hibernate generate this SQL and how do I fix it?

解决方案

According to the Spring Data spec you have to define this method as:

List<Instance> findByActionsIn(Collection<Action> actions);

这篇关于为什么Spring Data JPA + Hibernate生成不正确的SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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