程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了@ManyToMany JPA 2复杂查询大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决@manyToMany JPA 2复杂查询?

开发过程中遇到@manyToMany JPA 2复杂查询的问题如何解决?下面主要结合日常开发的经验,给出你关于@manyToMany JPA 2复杂查询的解决方法建议,希望对你解决@manyToMany JPA 2复杂查询有所启发或帮助;

这是可以提供帮助的查询:

SELEct c1 
from Class1 c1 
join c1.class2 c2 
where c2.ID in (1,2,3)
group by c1
having count(c1)=3 ;

解决方法

我有以下ManyToMany映射。

@Entity
public class Class1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@manyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "class1_class2",joincolumns = @Joincolumn(name = "class1Id"),inverseJoincolumns = @Joincolumn(name = "class2Id"))
private List<Class2> class2;
}

@Entity
public class Class2 {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}

我想检索与Classe2实体有关系的所有Class1实体,其中class2Id = 1和class2Id = 2和class2Id = 3。{1,2,3}

或者,要过滤在其class2列表上具有的Classe1实体,请使用具有以下值的Class2实体:class2Id = 1和class2Id =
2和class2Id = 3

例如:

如果在联接表上,我有以下值。

class1Id   class2Id
   1          1
   1          2
   1          3
   1          4
   6          1
   6          2
   6          3
   4          1
   5          2

对于该示例,结果将是Class1Id为1和6的Class1。因为Class1Id =
1的Class1实体的classe2Id为1,3,4,Class1Id = 2的Class1实体的classe2Id为1。 ,2,3

是否可以通过JPA2(谓词)获得正确的实体?

有没有更好的映射来处理这种情况?

目前,我提出了以下SQL查询:

SELEct v1.class1Id from class1_class2 v1
inner join class1_class2 v2 on v1.class1Id=v2.class1Id
inner join class1_class2 v3 on v2.class1Id=v3.class1Id
where v1.classe2Id=1 and v2.classe2Id=2 and v3.classe2Id=3;

大佬总结

以上是大佬教程为你收集整理的@ManyToMany JPA 2复杂查询全部内容,希望文章能够帮你解决@ManyToMany JPA 2复杂查询所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。