Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement jpa external join query join in Java

Shulou Source: shulou.com Published: 2022-06-03 18:25:18 10月03日 Update

Editor to share with you how to achieve jpa external join query join in Java. I hope you will get something after reading this article. Let's discuss it together.

1 、 IndexTagController.java

@ GetMapping ("/ tags/ {id}") public String types (@ PageableDefault (size = 3 PageableDefault sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @ PathVariable long id, Model model, HttpSession session) {/ / find all tags and sort List tags = tagService.listTagTop (50) by tag news volume If (id = =-1) {/ / get the classification of maximum data id = tags.get (0). GetId ();} model.addAttribute ("tags", tags); model.addAttribute ("page", newsService.listNews (id,pageable)); model.addAttribute ("activeId", id); session.setAttribute ("query", "); return" tags " }

Id is the id of the tag in newService.listNews (id,pgeable). All you need to do in this method is to query all the news that contains id as the parameter id in the tag.

2. Business layer code

NewService.java is an interface in which the following methods exist

/ / find eligible news Page listNews (long id,Pageable pageable) according to the tag Id

NewServiceImpl.java is a class that implements the NewService interface and implements the listNews method

@ Override public Page listNews (long id, Pageable pageable) {return newsRepository.findAll (new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery cq, CriteriaBuilder cb) {/ / external join query Join Join join = root.join ("tags"); return cb.equal (join.get ("id"), id) }, pageable);}

NewsRepository.java inherits JpaSpecificationExecutor

Public interface NewsRepository extends JpaRepository, JpaSpecificationExecutor {@ Query ("select n from News n where n.recommend = true") List findTop (Pageable pageable); @ Query ("select n from News n where n.title like? 1 or n.content like? 1") Page findByQuery (String query,Pageable pageable); @ Query ("select function ('date_format',n.updateTime,'%Y') as year1 from News n group by year1 order by year1 desc") List findGroupYear () @ Query ("select n from News n where function ('date_format',n.updateTime,'%Y') =? 1") List findByYear (String year);} after reading this article, I believe you have a certain understanding of "how to implement jpa external join query join in Java". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Tags queries news methods interfaces articles maximum business code parameters done that is data more conditions knowledge industry information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Microsoft Apple Redmi