jpa实现多对多的属性时查询的两种方法
作者:韩小文 时间:2022-01-14 00:47:45
jpa多对多的属性查询
第一:采用JPQL方式
使用@Query拼接jpql语句完成多对多的查询;
@query(
SELECT User FROM User u JOIN Student s on s.id = u.id where u.name LIKE :name
)
User findallByName(@param(“name”)String name);
第二:采用specification 方法
user中有List<student>属性,多对多的关系;
代码如下:
Specification<Class> specification = new Specification<>(){
@override
public predicate toPredicate(Root<Class> root , CriteriaQuery<?> query ,CriteriaBuilder cb){
List<Precidate> precidate = Lists.newArrayList<>();
if(num>0){
Predicate p1 = cb.equal(root.get("num").as(Integer.Class),count);
precidate.add(p1);
}
if(channel > 0){
ListJoin<User,Student> join= root.join(root.getModel().getList("student",student.class),JoinType.LEFT);
Predicate p2 = cb.equal(join.get("id").as(Integer.Class));
//这里面的join代表的是student,属于加入进来的部分,而不是链接表的全部结果;
precidate.add(p2);
}
List<User> list = DAO.findAll(specification,new Sort(DESC,"id"));
}
}
JPA,HQL多对多的查询语句
一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有.
要查询教拥有书"a"的学生的老师!
Hql语句
SELECT t FROM Teacher t join t.students s join s.books b where b.name = 'a'
解释:t.students s中s并不是集合的意思,而是t的students对象的表别名,join t.students s这个hql,hibernate会翻译成两个表的内连接关系
错误写法:
SELECT t FROM teacher t where t.students.books.name = 'a'
其实道理是很显然的,t.students是一个Set,那么这个Set怎么可能有books这样的属性呢?同理books.name也不对,所以使用表间连接并给出别名t.students s,此时的对象s才有books属性,所以可以写s.books b,最后使用b.name限制查询b.name = 'a'.
另外一种写法
SELECT t FROM Teacher t,Student s,Book b where s.id in elements(t.students) and b.id in elements(s.books)
这种方法没有出错!不过这种方式要用子查询!
来源:https://blog.csdn.net/han1196639488/article/details/60956310
标签:jpa,多对多,属性,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现图片转高斯模糊以及高斯模糊布局
2023-10-20 16:10:23
java基础教程之拼图游戏的实现
2022-05-02 21:22:21
![](https://img.aspxhome.com/file/2023/7/78227_0s.gif)
Spring FreeMarker整合Struts2过程详解
2023-03-01 00:30:52
![](https://img.aspxhome.com/file/2023/9/62169_0s.jpg)
Android如何实现一个DocumentProvider示例详解
2022-12-30 20:10:13
![](https://img.aspxhome.com/file/2023/5/102595_0s.jpg)
C# 设计模式系列教程-状态模式
2022-11-07 13:31:55
![](https://img.aspxhome.com/file/2023/9/111239_0s.png)
SpringBoot核心@SpringBootApplication使用介绍
2023-11-25 08:33:59
![](https://img.aspxhome.com/file/2023/2/59202_0s.png)
C#实现语音视频录制-附demo源码
2023-10-04 13:22:03
![](https://img.aspxhome.com/file/2023/8/91308_0s.png)
Java Synchronized的偏向锁详细分析
2021-07-09 00:35:23
![](https://img.aspxhome.com/file/2023/9/70969_0s.png)
JAVA回顾:封装,继承,多态
2021-09-26 20:17:59
![](https://img.aspxhome.com/file/2023/7/67627_0s.png)
基于Java ActiveMQ的实例讲解
2023-08-24 08:39:38
Mybatis映射文件实例详解
2023-06-12 13:08:42
SpringBoot实现Mysql使用MD5进行密码加密的示例
2022-11-24 23:14:16
![](https://img.aspxhome.com/file/2023/6/62156_0s.png)
Maven打包时如何指定启动类
2021-06-16 11:48:00
![](https://img.aspxhome.com/file/2023/2/67682_0s.png)
Spring五大类注解读取存储Bean对象的方法
2023-11-09 22:07:07
![](https://img.aspxhome.com/file/2023/3/59003_0s.png)
java如何反编译jar包并修改class文件重新打包
2021-09-20 05:24:26
![](https://img.aspxhome.com/file/2023/3/81933_0s.jpg)
集成apollo动态日志取缔logback-spring.xml配置
2021-10-15 16:53:19
图解红黑树及Java进行红黑二叉树遍历的方法
2023-04-20 22:09:14
![](https://img.aspxhome.com/file/2023/1/83021_0s.png)
一文搞懂JMeter engine中HashTree的配置问题
2022-05-11 01:21:08
![](https://img.aspxhome.com/file/2023/2/82022_0s.jpg)
一种类似JAVA线程池的C++线程池实现方法
2021-11-02 21:31:52
![](https://img.aspxhome.com/file/2023/7/79917_0s.png)
Java建造者设计模式详解
2022-09-19 13:14:11
![](https://img.aspxhome.com/file/2023/6/71236_0s.jpg)