How to implement hibernate named query

  • 2020-04-01 01:08:06
  • OfStack

What is a named query?
Hibernate allows you to define query statements in the form of strings in a mapping file, which is called a named query
What are the benefits of using named queries?
Because HQL using Hibernate often requires string queries to be written in Java code, HQL gets mixed up between code, destroying code readability, and by using named queries, you can separate business logic from query statements, allowing you to focus on queries without SQL or HQL code being scattered throughout the application.
You can use named queries to handle complex queries
How are named queries implemented?
Introduce the following ways:
Method one: In the configuration file < The class / > Under the tag, declare the query statement
 
<hibernate-mapping> 
<class name="com.test.bean.Student" table="student" catalog="users"> 
<id name="id" type="integer"> 
<column name="id" /> 
<generator class="identity" /> 
</id> 
<property name="name" type="string"> 
<column name="name" length="11" /> 
</property> 
<property name="age" type="integer"> 
<column name="age" /> 
</property> 
<property name="sex" type="string"> 
<column name="sex" length="2" /> 
</property> 
</class> 
<!--  Define query statements  --> 
<query name="findStudentByName"> 
<![CDATA[from Student where name = :name]]> 
</query> 
</hibernate-mapping> 

note : < ! [CDATA []] > What does that mean?
It's plain text, without that. > The &characters cannot be directly stored in XML and need to be escaped, whereas with this tag the symbols are stored in an XML document without escaping. You can avoid XML parsing errors caused by unexpected special symbols.
 
public List<Student> query(){ 
Session session = HibernateSessionFactory.getSession(); 
Query q = session.getNamedQuery("findStudentByName"); 
q.setString("name", "zhangsan"); 
List<Student> list = q.list(); 
return list; 
} 

Method 2 : also available in configuration files < The class / > Inside the tag, the query statement is declared, but the Java code needs to specify (package + class + configuration name) when invoked
 
<hibernate-mapping> 
<class name="com.test.bean.Student" table="student" catalog="users"> 
<id name="id" type="integer"> 
<column name="id" /> 
<generator class="identity" /> 
</id> 
<property name="name" type="string"> 
<column name="name" length="11" /> 
</property> 
<property name="age" type="integer"> 
<column name="age" /> 
</property> 
<property name="sex" type="string"> 
<column name="sex" length="2" /> 
</property> 
<!--  Define query statements  --> 
<query name="findStudentByName"> 
<![CDATA[from Student where name = :name]]> 
</query> 
</class> 
</hibernate-mapping> 
public List<Student> query(){ 
Session session = HibernateSessionFactory.getSession(); 
Query q = session.getNamedQuery("com.test.bean.Student.findStudentByName"); 
q.setString("name", "zhangsan"); 
List<Student> list = q.list(); 
return list; 
} 

Methods three : using native SQL queries < SQL - query> , you must write all the columns of the table in this way, otherwise you will get an error of 'invalid column name', unless you use return-scalar to set the field type.
 
<hibernate-mapping> 
<class name="com.test.bean.Student" table="student" catalog="users"> 
<id name="id" type="integer"> 
<column name="id" /> 
<generator class="identity" /> 
</id> 
<property name="name" type="string"> 
<column name="name" length="11" /> 
</property> 
<property name="age" type="integer"> 
<column name="age" /> 
</property> 
<property name="sex" type="string"> 
<column name="sex" length="2" /> 
</property> 
</class> 
<!--  Define query statements  --> 
<sql-query name="findStudentByName"> 
<return alias="s" class="com.test.bean.Student"> 
</return> 
<![CDATA[select {s.*} from student s where s.name = :name]]> 
</sql-query> 
</hibernate-mapping> 

note : also available in < The return / > Tag inside application < Return the property / > The tag lists all the fields of the table, just like the method described above, and queries all the columns.
The same code at the page code block index 1
Methods four : using native SQL queries < SQL - query> , if you apply return-scalar to set the field type, you will be able to query some of the fields.
 
<hibernate-mapping> 
<class name="com.test.bean.Student" table="student" catalog="users"> 
<id name="id" type="integer"> 
<column name="id" /> 
<generator class="identity" /> 
</id> 
<property name="name" type="string"> 
<column name="name" length="11" /> 
</property> 
<property name="age" type="integer"> 
<column name="age" /> 
</property> 
<property name="sex" type="string"> 
<column name="sex" length="2" /> 
</property> 
</class> 
<!--  Define query statements  --> 
<sql-query name="findStudentByName"> 
<return-scalar column="name" type="string"/> 
<return-scalar column="age" type="integer"/> 
<![CDATA[select s.name , s.age from student s where s.name = :name]]> 
</sql-query> 
</hibernate-mapping> 
public List<Object[]> query(){ 
Session session = HibernateSessionFactory.getSession(); 
Query query = session.getNamedQuery("findStudentByName"); 
query.setString("name", "zhangsan"); 
List<Object[]> list = query.list(); 
return list; 
} 

or :
 
public List<Student> query(){ 
Session session = HibernateSessionFactory.getSession(); 
Query q = session.getNamedQuery("findStudentByName"). 
setResultTransformer(Transformers.aliasToBean(Student.class)); 
q.setString("name", "zhangsan"); 
List<Student> list = q.list(); 
return list; 
} 

Related articles: