How Hibernate Implements Fuzzy Query

一、绑定参数法:



    Session session=HibernateUtil.getSessionFactory
    ().getCurrentSession();
    session.beginTransaction();
    String strSQL="from Classes as a where a.classno like :name";
    Query query = session.createQuery(strSQL);
    query.setString("name", "%"+OId+"%");
    List result=query.list();



    Session session=HibernateUtil.getSessionFactory 
    ().getCurrentSession();  
     
    session.beginTransaction(); 
     
    String strSQL="from Classes as a where a.classno like :name"; 
     
    Query query = session.createQuery(strSQL); 
            
    query.setString("name", "%"+OId+"%");  Second, in the object-oriented query language HQL
            
    List result=query.list(); 






    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List result=session.createQuery("from Classes as a where a.classno
    like " '%"+OId+"%'").list();




    Session session = HibernateUtil.getSessionFactory().getCurrentSession();  
        
    session.beginTransaction(); 
     
    List result=session.createQuery("from Classes as a where a.classno  
     
    like " '%"+OId+"%'").list(); 

三、模糊查询参数化



    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List result=session.createQuery("from Classes as a where a.classno
    like :name").setParameter("pid",OId).list();



    Session session=HibernateUtil.getSessionFactory().getCurrentSession(); 
     
    session.beginTransaction(); 
     
    List result=session.createQuery("from Classes as a where a.classno   
     
    like :name").setParameter("pid",OId). list(); 

Hibernate fuzzy query parameterization problem

from Project o where 1=1 and o.isDeleted=? and o.prjName like ? ; query.setString(i, "%"+actual query condition+"%"); Note that there are no single quotation marks on the left side of the first percent sign and the right side of the second percent sign in the parameter, which is usually written in SQL The sentences are different, so pay special attention. In addition, you should understand the difference between the two points

HQL: from Project o where 1=1 and PRJ_NAME like '%strCond%'; // Here PRJ_NAME should be the actual field name in the database table

HQL: from Project o where 1=1 and o.PRJ_NAME like '%strCond%'; //Here PRJ_NAME should be the attribute name of the entity class

HQL: from Project o where 1=1 and PRJ_NAME like '? ' ;//here? Not treated as placeholder parameter HQL: from Project o where 1=1 and PRJ_NAME like ? ;//When setting the parameter value, single quotation marks are automatically added around the parameter value.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993323&siteId=291194637