Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Criteria for grouping summation, sorting, and fuzzy query

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Criteria for group summation, sorting, fuzzy query", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Criteria for group summation, sorting, fuzzy query" this article.

Criteria performs grouping summation, sorting and fuzzy query.

Spring data is used in the engineering framework, but the JpaRepository and override JpaSpecificationExecutor interfaces provided by spring data do not meet my requirements. I will carry out fuzzy query, group the number according to bookId, and obtain the highest number of top n. Since fuzzy query is not a required condition, it is not appropriate to use @ Query annotation directly, so use Criteria to implement it.

The 1.Entity is as follows: package com.example.springdatatest.repository.entity; import lombok.Data; import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table; @ Table@Data@Entitypublic class TestEntity {@ Id String testId; @ Column public String regionCode; @ Column public long count; @ Column public String bookId; @ Column public String libraryCode;} 2.repository as follows: package com.example.springdatatest.repository Import com.example.springdatatest.repository.entity.TestEntity;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface TestRepository extends JpaRepository,JpaSpecificationExecutor {} 3.service is as follows: package com.example.springdatatest.service; import com.example.springdatatest.repository.TestRepository;import com.example.springdatatest.repository.entity.TestEntity;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.Tuple Import javax.persistence.criteria.*;import java.util.ArrayList;import java.util.List; @ Servicepublic class TestService {@ Autowired TestRepository testRepository; @ Autowired @ PersistenceContext private EntityManager entityManager; public void test () {CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder (); CriteriaQuery criteriaQuery = criteriaBuilder.createQuery (Tuple.class); Root root = criteriaQuery.from (TestEntity.class); List predicateList = new ArrayList () PredicateList.add (criteriaBuilder.equal (root.get ("libraryCode"), "1")); predicateList.add (criteriaBuilder.like (root.get ("regionCode"), "%" + "6101" + "%"); Predicate [] p = new Predicate [predicateList.size ()]; predicateList.toArray (p); Path bookId = root.get ("bookId"); Path bookCount = root.get ("count") CriteriaQuery.where (p) .multiselect (bookId,criteriaBuilder.sum (bookCount)) .groupBy (bookId) .orderBy (criteriaBuilder.desc (criteriaBuilder.sum (bookCount); List list = entityManager.createQuery (criteriaQuery) .setFirstResult (1) .setMaxResults (1) .getResultList ();}} 4. Incidentally, mention a small mistake inadvertently.

It is also due to carelessness that the sentence predicateList.toArray (p); is not written, which results in a null pointer exception all the time.

Java.lang.NullPointerException

At org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render (CompoundPredicate.java:166)

At org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render (CompoundPredicate.java:115)

At org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render (CompoundPredicate.java:105)

At org.hibernate.query.criteria.internal.QueryStructure.render (QueryStructure.java:248)

At org.hibernate.query.criteria.internal.CriteriaQueryImpl.interpret (CriteriaQueryImpl.java:292)

At org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile (CriteriaCompiler.java:149)

At org.hibernate.internal.SessionImpl.createQuery (SessionImpl.java:3707)

At org.hibernate.internal.SessionImpl.createQuery (SessionImpl.java:208)

At sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)

At sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)

At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)

At java.lang.reflect.Method.invoke (Method.java:498)

At org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke (ExtendedEntityManagerCreator.java:350)

At com.sun.proxy.$Proxy81.createQuery (Unknown Source)

At sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)

At sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)

At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)

At java.lang.reflect.Method.invoke (Method.java:498)

At org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke (SharedEntityManagerCreator.java:309)

At com.sun.proxy.$Proxy81.createQuery (Unknown Source)

At com.example.springdatatest.service.TestService.test (TestService.java:50)

At com.example.springdatatest.SpringdatatestApplicationTests.contextLoads (SpringdatatestApplicationTests.java:19)

At sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)

At sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)

At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)

At java.lang.reflect.Method.invoke (Method.java:498)

At org.junit.runners.model.FrameworkMethod$1.runReflectiveCall (FrameworkMethod.java:50)

At org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:12)

At org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:47)

At org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:17)

At org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate (RunBeforeTestExecutionCallbacks.java:74)

At org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate (RunAfterTestExecutionCallbacks.java:84)

At org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate (RunBeforeTestMethodCallbacks.java:75)

At org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate (RunAfterTestMethodCallbacks.java:86)

At org.springframework.test.context.junit4.statements.SpringRepeat.evaluate (SpringRepeat.java:84)

At org.junit.runners.ParentRunner.runLeaf (ParentRunner.java:325)

At org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (SpringJUnit4ClassRunner.java:251)

At org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (SpringJUnit4ClassRunner.java:97)

At org.junit.runners.ParentRunner$3.run (ParentRunner.java:290)

At org.junit.runners.ParentRunner$1.schedule (ParentRunner.java:71)

At org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288)

At org.junit.runners.ParentRunner.access$000 (ParentRunner.java:58)

At org.junit.runners.ParentRunner$2.evaluate (ParentRunner.java:268)

At org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate (RunBeforeTestClassCallbacks.java:61)

At org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate (RunAfterTestClassCallbacks.java:70)

At org.junit.runners.ParentRunner.run (ParentRunner.java:363)

At org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (SpringJUnit4ClassRunner.java:190)

At org.junit.runner.JUnitCore.run (JUnitCore.java:137)

At com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (JUnit4IdeaTestRunner.java:68)

At com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs (IdeaTestRunner.java:47)

At com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (JUnitStarter.java:242)

At com.intellij.rt.execution.junit.JUnitStarter.main (JUnitStarter.java:70)

This is only a small demo implemented, the specific input parameters are not reflected, make a record here.

Criteria carries out fuzzy query to realize the function of intra-site search.

Today, a new function of intra-site search has been added to the website. The idea is to use Criteria for fuzzy query.

The method of Dao layer is as follows: / / search method public List findSearch (String scon) {Session s = getHibernateTemplate (). GetSessionFactory (). OpenSession (); Criteria criteria = s.createCriteria (Question.class); criteria.add (Expression.like ("qdesc", "%" + scon+ "%"), Expression.like ("qname", "%" + scon+ "%")); return criteria.list () } these are all the contents of the article "how to use Criteria for grouping summation, sorting, and fuzzy query". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report