Hibernate named query examples
来源:互联网 发布:更改mac屏幕下面的图标 编辑:程序博客网 时间:2024/05/20 20:20
Often times, developer like to put HQL string literals scatter all over the Java code, this method is hard to maintaine and look ugly. Fortunately, Hibernate come out a technique called “names queries” , it lets developer to put all HQL into the XML mapping file or via annotation.
How to declare named query
The named query is supported in both HQL or native SQL. see examples…
1. XML mapping file
HQL in mapping file
<!-- stock.hbm.xml --><hibernate-mapping> <class name="com.mkyong.common.Stock" table="stock" ...> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="identity" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> ... </class> <query name="findStockByStockCode"> <![CDATA[from Stock s where s.stockCode = :stockCode]]> </query></hibernate-mapping>
Native SQL in mapping file
<!-- stock.hbm.xml --><hibernate-mapping> <class name="com.mkyong.common.Stock" table="stock" ...> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="identity" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> ... </class> <sql-query name="findStockByStockCodeNativeSQL"> <return alias="stock" class="com.mkyong.common.Stock"/> <![CDATA[select * from stock s where s.stock_code = :stockCode]]> </sql-query></hibernate-mapping>
You can place a named query inside ‘hibernate-mapping
‘ element, but do not put before the ‘class
‘ element, Hibernate will prompt invalid mapping file, all your named queries have to put after the ‘class
‘ element.
Note
Regarding theCDATA
, it’s always good practice to wrap your query text with CDATA, so that the XML parser will not prompt error for some special XML characters like ‘>
’ ,<
‘ and etc.
2. Annotation
HQL in annotation
@NamedQueries({ @NamedQuery( name = "findStockByStockCode", query = "from Stock s where s.stockCode = :stockCode" )})@Entity@Table(name = "stock", catalog = "mkyong")public class Stock implements java.io.Serializable {...
Native SQL in annotation
@NamedNativeQueries({ @NamedNativeQuery( name = "findStockByStockCodeNativeSQL", query = "select * from stock s where s.stock_code = :stockCode", resultClass = Stock.class )})@Entity@Table(name = "stock", catalog = "mkyong")public class Stock implements java.io.Serializable {...
In native SQL, you have to declare the ‘resultClass
‘ to let Hibernate know what is the return type, failed to do it will caused the exception “org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
“.
Call a named query
In Hibernate, you can call the named query via getNamedQuery
method.
Query query = session.getNamedQuery("findStockByStockCode").setString("stockCode", "7277");Query query = session.getNamedQuery("findStockByStockCodeNativeSQL").setString("stockCode", "7277");
Conclusion
Named queries are global access, which means the name of a query have to be unique in XML mapping files or annotations. In real environment, it’s always good practice to isolate all the named queries into their own file. In addition, named queries stored in the Hibernate mapping files or annotation are more easier to maintain than queries scattered through the Java code.
- Hibernate named query examples
- Hibernate Query examples (HQL)
- Hibernate Error in named query (Unknown entity)
- Hibernate:Named query not known异常的一种解决方案
- An Example for Hibernate Named Query with Aggregate Function
- org.hibernate.MappingException: Named query not known: XXXX 解决办法
- Hibernate Examples
- Hierarchical Query Examples
- JPA Criteria Query Examples
- Named query not known: *
- Hibernate外置命名查询报错:Named query not known: serachStu
- Spring Named Parameters examples in SimpleJdbcTemplate
- Spring Named Parameters examples in SimpleJdbcTemplate
- ImportError: No module named examples.tutorials.mnist
- ImportError: No module named examples.tutorials.mnist
- ImportError: No module named examples.tutorials.mnist
- Hibernate parameter binding examples
- Hibernate Criteria examples
- JAVA实现简单四则混合运算
- m802 codec无法录音补丁
- jave类型转换
- Android 不要把android做成ios!
- 基于百度地图定位SDK实现定位
- Hibernate named query examples
- Linux写时拷贝技术(copy-on-write)
- 再来看两个包装,一个是context的包装,一个是contexttheme的包装
- android 第二更(后台判断网络状态,反馈到页面)
- NMAP 扫描端口速度快
- android 系统优化包括哪些方面
- mongodb查询不重复数据的记录总数
- Jodd瑞士军刀
- html checkbox attr and prop