What is a named query?
Hibernate allows you to define query statements in the form of strings in the mapping file. This query method is called a named query.
What are the benefits of using named queries?
Since using Hibernate's HQL often requires writing string query statements in Java code, HQL is mixed between codes, destroying code readability. By using named queries, you can separate business logic and query statements, allowing you to focus on queries. This avoids the situation where SQL or HQL code is scattered throughout the application.
Named queries can be used to process complex queries
How to implement named query?
Introducing the following methods:
Method 1: Declare the query statement under the <class/> tag in the configuration file
Copy the code code as follows:
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator />
</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 statement-->
<query name="findStudentByName">
<![CDATA[from Student where name = :name]]>
</query>
</hibernate-mapping>
Note : What does <![CDATA[ ]]> mean?
Indicates that it is plain text. Without this, the < > & characters cannot be directly stored in XML and need to be escaped. However, with this tag, these symbols can be stored in XML documents without escaping. This can avoid unexpected special symbols causing XML parsing errors.
Copy the code code as follows:
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 : You can also declare the query statement in the <class/> tag in the configuration file, but you need to specify it when calling the java code (package + class + configuration name)
Copy the code code as follows:
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator />
</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 statement-->
<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;
}
Method three : Use native sql query <sql-query>. In this method, all columns of the table must be written, otherwise an 'invalid column name' error will occur unless you use return-scalar to set the field type.
Copy the code code as follows:
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator />
</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 statement-->
<sql-query name="findStudentByName">
<return alias="s">
</return>
<![CDATA[select {s.*} from student s where s.name = :name]]>
</sql-query>
</hibernate-mapping>
Note : You can also use the <return-property/> tag inside the <return/> tag to list all fields of the table. The same as the method described above, all columns are queried.
Copy the code code as follows:
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 4 : Use native sql query <sql-query>. If you use return-scalar to set the field type, you can query some fields.
Copy the code code as follows:
<hibernate-mapping>
<class name="com.test.bean.Student" table="student" catalog="users">
<id name="id" type="integer">
<column name="id" />
<generator />
</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 statement-->
<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 :
Copy the code code as follows:
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;
}