⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 querycriteria.html

📁 是一个中文的Hibernate库文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;16&nbsp;&#31456;&nbsp;    &#26465;&#20214;&#26597;&#35810;(Criteria Queries)    </title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="up" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="previous" href="queryhql.html" title="&#31532;&nbsp;15&nbsp;&#31456;&nbsp;HQL: Hibernate&#26597;&#35810;&#35821;&#35328;"><link rel="next" href="querysql.html" title="&#31532;&nbsp;17&nbsp;&#31456;&nbsp;Native SQL&#26597;&#35810;"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">&#31532;&nbsp;16&nbsp;&#31456;&nbsp;    &#26465;&#20214;&#26597;&#35810;(Criteria Queries)    </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="queryhql.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="querysql.html">&#19979;&#19968;&#39029;</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="querycriteria"></a>&#31532;&nbsp;16&nbsp;&#31456;&nbsp;    &#26465;&#20214;&#26597;&#35810;(Criteria Queries)    </h2></div></div><div></div></div><p>        &#20855;&#26377;&#19968;&#20010;&#30452;&#35266;&#30340;&#12289;&#21487;&#25193;&#23637;&#30340;&#26465;&#20214;&#26597;&#35810;API&#26159;Hibernate&#30340;&#29305;&#33394;&#12290;    </p><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="querycriteria-creating"></a>16.1.&nbsp;&#21019;&#24314;&#19968;&#20010;<tt class="literal">Criteria</tt> &#23454;&#20363;</h2></div></div><div></div></div><p>            <tt class="literal">org.hibernate.Criteria</tt>&#25509;&#21475;&#34920;&#31034;&#29305;&#23450;&#25345;&#20037;&#31867;&#30340;&#19968;&#20010;&#26597;&#35810;&#12290;<tt class="literal">Session</tt>&#26159;            <tt class="literal">Criteria</tt>&#23454;&#20363;&#30340;&#24037;&#21378;&#12290;        </p><pre class="programlisting">Criteria crit = sess.createCriteria(Cat.class);crit.setMaxResults(50);List cats = crit.list();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="querycriteria-narrowing"></a>16.2.&nbsp;&#38480;&#21046;&#32467;&#26524;&#38598;&#20869;&#23481;</h2></div></div><div></div></div><p>            &#19968;&#20010;&#21333;&#29420;&#30340;&#26597;&#35810;&#26465;&#20214;&#26159;<tt class="literal">org.hibernate.criterion.Criterion</tt>            &#25509;&#21475;&#30340;&#19968;&#20010;&#23454;&#20363;&#12290;<tt class="literal">org.hibernate.criterion.Restrictions</tt>&#31867;            &#23450;&#20041;&#20102;&#33719;&#24471;&#26576;&#20123;&#20869;&#32622;<tt class="literal">Criterion</tt>&#31867;&#22411;&#30340;&#24037;&#21378;&#26041;&#27861;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.like("name", "Fritz%") )    .add( Restrictions.between("weight", minWeight, maxWeight) )    .list();</pre><p>            &#32422;&#26463;&#21487;&#20197;&#25353;&#36923;&#36753;&#20998;&#32452;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.like("name", "Fritz%") )    .add( Restrictions.or(        Restrictions.eq( "age", new Integer(0) ),        Restrictions.isNull("age")    ) )    .list();</pre><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) )    .add( Restrictions.disjunction()        .add( Restrictions.isNull("age") )    	.add( Restrictions.eq("age", new Integer(0) ) )    	.add( Restrictions.eq("age", new Integer(1) ) )    	.add( Restrictions.eq("age", new Integer(2) ) )    ) )    .list();</pre><p>            Hibernate&#25552;&#20379;&#20102;&#30456;&#24403;&#22810;&#30340;&#20869;&#32622;criterion&#31867;&#22411;(<tt class="literal">Restrictions</tt>            &#23376;&#31867;), &#20294;&#26159;&#23588;&#20854;&#26377;&#29992;&#30340;&#26159;&#21487;&#20197;&#20801;&#35768;&#20320;&#30452;&#25509;&#20351;&#29992;SQL&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.sql("lower({alias}.name) like lower(?)", "Fritz%", Hibernate.STRING) )    .list();</pre><p>            <tt class="literal">{alias}</tt>&#21344;&#20301;&#31526;&#24212;&#24403;&#34987;&#26367;&#25442;&#20026;&#34987;&#26597;&#35810;&#23454;&#20307;&#30340;&#21015;&#21035;&#21517;&#12290;        </p><p>                         <tt class="literal">Property</tt>&#23454;&#20363;&#26159;&#33719;&#24471;&#19968;&#20010;&#26465;&#20214;&#30340;&#21478;&#22806;&#19968;&#31181;&#36884;&#24452;&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#35843;&#29992;<tt class="literal">Property.forName()</tt>            &#21019;&#24314;&#19968;&#20010;<tt class="literal">Property</tt>&#12290;                   </p><pre class="programlisting">Property age = Property.forName("age");List cats = sess.createCriteria(Cat.class)    .add( Restrictions.disjunction()        .add( age.isNull() )    	.add( age.eq( new Integer(0) ) )    	.add( age.eq( new Integer(1) ) )    	.add( age.eq( new Integer(2) ) )    ) )    .add( Property.forName("name").in( new String[] { "Fritz", "Izi", "Pk" } ) )    .list();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="querycriteria-ordering"></a>16.3.&nbsp;&#32467;&#26524;&#38598;&#25490;&#24207;</h2></div></div><div></div></div><p>            &#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">org.hibernate.criterion.Order</tt>&#26469;&#20026;&#26597;&#35810;&#32467;&#26524;&#25490;&#24207;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.like("name", "F%")    .addOrder( Order.asc("name") )    .addOrder( Order.desc("age") )    .setMaxResults(50)    .list();</pre><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Property.forName("name").like("F%") )    .addOrder( Property.forName("name").asc() )    .addOrder( Property.forName("age").desc() )    .setMaxResults(50)    .list();</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="querycriteria-associations"></a>16.4.&nbsp;&#20851;&#32852;</h2></div></div><div></div></div><p>            &#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">createCriteria()</tt>&#38750;&#24120;&#23481;&#26131;&#30340;&#22312;&#20114;&#30456;&#20851;&#32852;&#30340;&#23454;&#20307;&#38388;&#24314;&#31435;            &#32422;&#26463;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.like("name", "F%")    .createCriteria("kittens")        .add( Restrictions.like("name", "F%")    .list();</pre><p>            &#27880;&#24847;&#31532;&#20108;&#20010; <tt class="literal">createCriteria()</tt>&#36820;&#22238;&#19968;&#20010;&#26032;&#30340;            <tt class="literal">Criteria</tt>&#23454;&#20363;&#65292;&#35813;&#23454;&#20363;&#24341;&#29992;<tt class="literal">kittens</tt> &#38598;&#21512;&#20013;&#30340;&#20803;&#32032;&#12290;        </p><p>            &#25509;&#19979;&#26469;&#65292;&#26367;&#25442;&#24418;&#24577;&#22312;&#26576;&#20123;&#24773;&#20917;&#19979;&#20063;&#26159;&#24456;&#26377;&#29992;&#30340;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .createAlias("kittens", "kt")    .createAlias("mate", "mt")    .add( Restrictions.eqProperty("kt.name", "mt.name") )    .list();</pre><p>                        (<tt class="literal">createAlias()</tt>&#24182;&#19981;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;            <tt class="literal">Criteria</tt>&#23454;&#20363;&#12290;)        </p><p>            <tt class="literal">Cat</tt>&#23454;&#20363;&#25152;&#20445;&#23384;&#30340;&#20043;&#21069;&#20004;&#27425;&#26597;&#35810;&#25152;&#36820;&#22238;&#30340;kittens&#38598;&#21512;&#26159;            <span class="emphasis"><em>&#27809;&#26377;</em></span>&#34987;&#26465;&#20214;&#39044;&#36807;&#28388;&#30340;&#12290;&#22914;&#26524;&#20320;&#24076;&#26395;&#21482;&#33719;&#24471;&#31526;&#21512;&#26465;&#20214;&#30340;kittens&#65292;            &#20320;&#24517;&#39035;&#20351;&#29992;<tt class="literal">returnMaps()</tt>&#12290;                    </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .createCriteria("kittens", "kt")        .add( Restrictions.eq("name", "F%") )    .returnMaps()    .list();Iterator iter = cats.iterator();while ( iter.hasNext() ) {    Map map = (Map) iter.next();    Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);    Cat kitten = (Cat) map.get("kt");}</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="querycriteria-dynamicfetching"></a>16.5.&nbsp;&#21160;&#24577;&#20851;&#32852;&#25235;&#21462;</h2></div></div><div></div></div><p>            &#20320;&#21487;&#20197;&#20351;&#29992;<tt class="literal">setFetchMode()</tt>&#22312;&#36816;&#34892;&#26102;&#23450;&#20041;&#21160;&#24577;&#20851;&#32852;&#25235;&#21462;&#30340;&#35821;&#20041;&#12290;        </p><pre class="programlisting">List cats = sess.createCriteria(Cat.class)    .add( Restrictions.like("name", "Fritz%") )    .setFetchMode("mate", FetchMode.EAGER)    .setFetchMode("kittens", FetchMode.EAGER)    .list();</pre><p>            &#36825;&#20010;&#26597;&#35810;&#21487;&#20197;&#36890;&#36807;&#22806;&#36830;&#25509;&#25235;&#21462;<tt class="literal">mate</tt>&#21644;<tt class="literal">kittens</tt>&#12290;           &#26597;&#30475;<a href="performance.html#performance-fetching" title="20.1.&nbsp;&#xA;	&#xA;			&#25235;&#21462;&#31574;&#30053;(Fetching strategies)&#xA;		">&#31532;&nbsp;20.1&nbsp;&#33410; &#8220;	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -