Hibernate的命名查询(NamedQuery)
打開Hibernate的Session的源碼,我們可以看到有一個getNamedQuery(String name);的方法,如下:
/*** Obtain an instance of <tt>Query</tt> for a named query string defined in the* mapping file.** @param queryName the name of a query defined externally* @return Query* @throws HibernateException*/public Query getNamedQuery(String queryName) throws HibernateException;?
這個方法就是可以執(zhí)行定義在Hibernate實體類映射文件中的HQL或Sql語句。
具體做法下面我一一講解,首先測試在映射文件中定義HQL語句。
我在User實體類的映射文件中定義了一個name為getUserByName的HQL查詢語句,代碼如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""D:\tools\hibernate\dtd\hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.xigua.domain"><class name="User"><id name="id"><generator class="native"/></id><property name="name"/><property name="birthday"/></class><!-- 定義一個查詢,名稱為getUserByName --><query name="getUserByName"><![CDATA[from User where name = :name]]></query> </hibernate-mapping>?
然后編寫測試類使用Session中的getNamedQuery(String name)方法進行測試,代碼如下:
package com.xigua.test;import java.util.Date; import java.util.List;import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;import com.xigua.domain.User; import com.xigua.utils.HibernateUtil;public class Test9 {public static void main(String[] args) {addUser();String name = "xigua";List<User> list = namedQuery(name);if(list != null && !list.isEmpty()) {for(User user : list) {System.out.println(user.getId() + ", " + user.getName() + ", " + user.getBirthday());}}}public static void addUser() {Session session = null;Transaction tx = null;try{session = HibernateUtil.getSession();tx = session.beginTransaction();User user = new User();user.setName("xigua");user.setBirthday(new Date());session.save(user);user = new User();user.setName("donggua");user.setBirthday(new Date());session.save(user);tx.commit();} catch(Exception e) {if(tx != null) {tx.rollback();}} finally {if(session != null) {session.close();}}}public static List<User> namedQuery(String name) {Session session = null;try{session = HibernateUtil.getSession();Query query = session.getNamedQuery("getUserByName");query.setParameter("name", name);return query.list();}catch(Exception e) {e.printStackTrace();} finally {if(session != null) {session.close();}}return null;}}?注意紅色部分代碼。
?
在映射文件中除了可以定義HQL語句,也還是可以定義Sql語句的。
前面的User實體類的映射文件修改如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""D:\tools\hibernate\dtd\hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.xigua.domain"><class name="User"><id name="id"><generator class="native"/></id><property name="name"/><property name="birthday"/></class><!-- 定義一個查詢,名稱為getUserByName <query name="getUserByName"><![CDATA[from User where name = :name]]></query>--><sql-query name="getUserByName"><![CDATA[select id, name from user where name = :name]]></sql-query> </hibernate-mapping>?
測試代碼如下:
package com.xigua.test;import java.util.Date;import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;import com.xigua.domain.User; import com.xigua.utils.HibernateUtil;public class Test10 {public static void main(String args[]) {addUser();String name = "xigua";User user = namedSqlQuery(name);if(user != null) {System.out.println(user.getId() + ", " + user.getName());}}public static void addUser() {Session session = null;Transaction tx = null;try{session = HibernateUtil.getSession();tx = session.beginTransaction();User user = new User();user.setName("xigua");user.setBirthday(new Date());session.save(user);user = new User();user.setName("donggua");user.setBirthday(new Date());session.save(user);tx.commit();} catch(Exception e) {if(tx != null) {tx.rollback();}} finally {if(session != null) {session.close();}}}public static User namedSqlQuery(String name) {Session session = null;try{session = HibernateUtil.getSession();Query query = session.getNamedQuery("getUserByName");query.setParameter("name", name);Object[] obj = (Object[]) query.uniqueResult();if(obj != null) {User user = new User();user.setId(Long.valueOf(obj[0].toString()));user.setName(obj[1].toString());return user;}}catch(Exception e) {e.printStackTrace();} finally {if(session != null) {session.close();}}return null;}}?
映射文件中還有些其它的配置,暫時還沒具體搞懂,比如下面紅色的代碼:
<sql-query name="getUserByName"><![CDATA[select id, name from user where name = :name]]><query-param name="name" type="string"/><return></return><return-join alias="" property=""></return-join><return-scalar column=""/><synchronize table=""/></sql-query>?
?
還有一點像說明的是<query>跟<sql-query>的配置是可以放到<class>節(jié)點里面的。
我現(xiàn)在是放在<class>節(jié)點外面,表示全局可用,這里需要注意不要跟其它映射文件中定義的<query>或<sql-query>同名。
如果將<query>或<sql-query>放在<class>節(jié)點里面,在java代碼中使用的時候需要將package名跟class名都寫上,具體見下面配置的映射文件代碼跟測試代碼。
映射文件代碼:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""D:\tools\hibernate\dtd\hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.xigua.domain"><class name="User"><id name="id"><generator class="native"/></id><property name="name"/><property name="birthday"/><!-- 定義一個查詢,名稱為getUserByName (這里將<query>放到<class>節(jié)點里面來)--> <query name="getUserByName"><![CDATA[from User where name = :name]]></query></class></hibernate-mapping>?java測試代碼(注意下圖紅色部分字符串)
package com.xigua.test;import java.util.Date; import java.util.List;import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;import com.xigua.domain.User; import com.xigua.utils.HibernateUtil;public class Test9 {public static void main(String[] args) {addUser();String name = "xigua";List<User> list = namedQuery(name);if(list != null && !list.isEmpty()) {for(User user : list) {System.out.println(user.getId() + ", " + user.getName() + ", " + user.getBirthday());}}}public static void addUser() {Session session = null;Transaction tx = null;try{session = HibernateUtil.getSession();tx = session.beginTransaction();User user = new User();user.setName("xigua");user.setBirthday(new Date());session.save(user);user = new User();user.setName("donggua");user.setBirthday(new Date());session.save(user);tx.commit();} catch(Exception e) {if(tx != null) {tx.rollback();}} finally {if(session != null) {session.close();}}}public static List<User> namedQuery(String name) {Session session = null;try{session = HibernateUtil.getSession();Query query = session.getNamedQuery("com.xigua.domain.User.getUserByName");query.setParameter("name", name);return query.list();}catch(Exception e) {e.printStackTrace();} finally {if(session != null) {session.close();}}return null;}}轉(zhuǎn)載于:https://www.cnblogs.com/limengtr/p/4074594.html
總結(jié)
以上是生活随笔為你收集整理的Hibernate的命名查询(NamedQuery)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下svn重新定位的方法
- 下一篇: UITableView刷新单个cell或