From: <one...@us...> - 2003-03-02 13:03:17
|
Update of /cvsroot/hibernate/Hibernate2/doc/reference/src In directory sc8-pr-cvs1:/tmp/cvs-serv7909 Modified Files: query_language.xml Log Message: documented latest QL changes Index: query_language.xml =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/doc/reference/src/query_language.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** query_language.xml 1 Mar 2003 13:02:19 -0000 1.4 --- query_language.xml 2 Mar 2003 13:03:15 -0000 1.5 *************** *** 13,18 **** <literal>net.sf.hibernate.eg.FOO</literal> is not <literal>net.sf.hibernate.eg.Foo</literal> and ! <literal>foo.Bar</literal> is not ! <literal>foo.BAR</literal>. </para> </sect1> --- 13,18 ---- <literal>net.sf.hibernate.eg.FOO</literal> is not <literal>net.sf.hibernate.eg.Foo</literal> and ! <literal>foo.barSet</literal> is not ! <literal>foo.BARSET</literal>. </para> </sect1> *************** *** 26,48 **** </para> ! <programlisting><![CDATA[from cat in class eg.Cat]]></programlisting> <para> which simply returns all instances of the class <literal>eg.Cat</literal>. The query assigns the alias <literal>cat</literal> to <literal>Cat</literal> ! instances, so we could use that alias later in the query. </para> </sect1> <sect1 id="query-language-s3"> <title>The select clause</title> <para> ! A multipart <literal>from</literal> clause is possible. The <literal>select</literal> ! clause picks which classes to return in the result set. Consider: </para> ! <programlisting><![CDATA[select mate from cat in class eg.Cat, mate in class eg.Cat ! where mate = cat.mate]]></programlisting> <para> --- 26,112 ---- </para> ! <programlisting><![CDATA[from eg.Cat as cat]]></programlisting> <para> which simply returns all instances of the class <literal>eg.Cat</literal>. The query assigns the alias <literal>cat</literal> to <literal>Cat</literal> ! instances, so we could use that alias later in the query. The <literal>as</literal> ! keyword is optional; we could also write: </para> + + <programlisting><![CDATA[from eg.Cat cat]]></programlisting> + + <para> + Multiple classes may appear, resulting in a cartesian product or "cross" join. + </para> + + <programlisting><![CDATA[from Formula as form, Parameter as param]]></programlisting> + </sect1> <sect1 id="query-language-s3"> + <title>Associations and joins</title> + + <para> + We may also define aliases to associated entities using a <literal>join</literal>. + </para> + + <programlisting><![CDATA[from eg.Cat as cat + inner join cat.mate as mate + left outer join cat.kittens as kitten + + from eg.Cat as cat left join cat.mate.kittens as kittens + + from Formula form full join form.parameter param]]></programlisting> + + <para> + The supported join types are borrowed from ANSI SQL + </para> + + <itemizedlist spacing="compact"> + <listitem> + <para> + <literal>inner join</literal> + </para> + </listitem> + <listitem> + <para> + <literal>left outer join</literal> + </para> + </listitem> + <listitem> + <para> + <literal>right outer join</literal> (not recommended) + </para> + </listitem> + <listitem> + <para> + <literal>full join</literal> (not usually useful) + </para> + </listitem> + </itemizedlist> + + <para> + The <literal>inner join</literal>, <literal>left outer join</literal> and + <literal>right outer join</literal> constructs may be abbreviated. + </para> + + <programlisting><![CDATA[from eg.Cat as cat + join cat.mate as mate + left join cat.kittens as kitten]]></programlisting> + + </sect1> + + <sect1 id="query-language-s4"> <title>The select clause</title> <para> ! The <literal>select</literal> clause picks which objects and properties to return in ! the query result set. Consider: </para> ! <programlisting><![CDATA[select mate ! from eg.Cat cat ! inner join cat.mate cat]]></programlisting> <para> *************** *** 51,55 **** </para> ! <programlisting><![CDATA[select cat.mate from cat in class eg.Cat]]></programlisting> <para> --- 115,119 ---- </para> ! <programlisting><![CDATA[select cat.mate from eg.Cat cat]]></programlisting> <para> *************** *** 58,62 **** </para> ! <programlisting><![CDATA[select elements(cat.kittens) from cat in class eg.Cat]]></programlisting> <para> --- 122,126 ---- </para> ! <programlisting><![CDATA[select elements(cat.kittens) from eg.Cat cat]]></programlisting> <para> *************** *** 64,90 **** </para> ! <programlisting><![CDATA[select cat.name from cat in class eg.DomesticCat where cat.name like 'fri%' ! select cust.name.firstName from cust in class Customer]]></programlisting> <para> ! You may select multiple objects (Hibernate will return them in an array of type ! <literal>Object[]</literal>) </para> ! <programlisting><![CDATA[select mother, offspr ! from mother in class eg.DomesticCat, offspr in class eg.Cat ! where offspr in elements(mother.kittens)]]></programlisting> <para> ! Queryies may even return aggregate functions of properties. Collections may also appear ! inside aggregate functions in the <literal>select</literal> clause. </para> <programlisting><![CDATA[select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat) ! from cat in class eg.Cat ! select cat, count( elements(cat.kittens) ) from cat in class eg.Cat group by cat]]></programlisting> <para> --- 128,177 ---- </para> ! <programlisting><![CDATA[select cat.name from eg.DomesticCat cat where cat.name like 'fri%' ! select cust.name.firstName from Customer as cust]]></programlisting> <para> ! Queries may return multiple objects and/or properties as an array of type ! <literal>Object[]</literal> </para> ! <programlisting><![CDATA[select cat, offspr, mate.name ! from eg.DomesticCat as mother ! inner join mother.mate as mate ! left outer join mother.kittens as offspr]]></programlisting> ! ! <para> ! or as an actual typesafe Java object ! </para> ! ! <programlisting><![CDATA[select new Family(cat, mate, offspr) ! from eg.DomesticCat as mother ! join mother.mate as mate ! left join mother.kittens as offspr]]></programlisting> ! ! <para> ! assuming that the class <literal>Family</literal> has an appropriate constructor. ! </para> ! ! </sect1> + <sect1 id="query-language-s4a"> + <title>Aggregate functions</title> <para> ! Queryies may even return aggregate functions of properties. </para> <programlisting><![CDATA[select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat) ! from eg.Cat cat></programlisting> ! <para> ! Collections may also appear inside aggregate functions in the <literal>select</literal> ! clause. ! </para> ! ! <programlisting><![CDATA[select cat, count( elements(cat.kittens) ) ! from eg.Cat cat group by cat]]></programlisting> <para> *************** *** 114,140 **** the same semantics as in SQL. </para> ! <programlisting><![CDATA[select distinct cat.name from cat in class eg.Cat]]></programlisting> </sect1> ! <sect1 id="query-language-s3b"> <title>polymorphism</title> <para> A query like: </para> ! <programlisting><![CDATA[from cat in class eg.Cat]]></programlisting> <para> returns instances not only of <literal>Cat</literal>, but also of subclasses like ! <literal>DomesticCat</literal>. Hibernate queries may name <emphasis>any</emphasis> ! Java class or interface in the <literal>from</literal> clause. The query will ! return instances of all persistent classes that extend that class or implement ! the interface. The following query would return all persistent objects: </para> ! <programlisting><![CDATA[from o in class java.lang.Object]]></programlisting> <para> The interface <literal>Named</literal> might be implemented by various persistent classes: </para> ! <programlisting><![CDATA[from n in class eg.Named, m in class eg.Named where n.name = m.name]]></programlisting> <para> Note that these last two queries will require more than one SQL <literal>SELECT</literal>. This --- 201,233 ---- the same semantics as in SQL. </para> ! <programlisting><![CDATA[select distinct cat.name from eg.Cat cat ! ! select count(distinct cat.name), count(cat) from eg.Cat cat]]></programlisting> </sect1> ! <sect1 id="query-language-s4b"> <title>polymorphism</title> <para> A query like: </para> ! <programlisting><![CDATA[from eg.Cat as cat]]></programlisting> <para> returns instances not only of <literal>Cat</literal>, but also of subclasses like ! <literal>DomesticCat</literal>. Hibernate queries may name <emphasis>any</emphasis> Java ! class or interface in the <literal>from</literal> clause. The query will return instances ! of all persistent classes that extend that class or implement the interface. The following ! query would return all persistent objects: </para> ! ! <programlisting><![CDATA[from java.lang.Object o]]></programlisting> ! <para> The interface <literal>Named</literal> might be implemented by various persistent classes: </para> ! ! <programlisting><![CDATA[from eg.Named n, eg.Named m where n.name = m.name]]></programlisting> ! <para> Note that these last two queries will require more than one SQL <literal>SELECT</literal>. This *************** *** 142,173 **** (It also means you can't call these queries using <literal>Query.scroll()</literal>.) </para> </sect1> - <sect1 id="query-language-s4"> - <title>from collections</title> - - <para> - You can select entities (but not values) from a collection - </para> - - <programlisting><![CDATA[select kitten from - cat in class eg.Cat, - kitten in elements(cat.kittens)]]></programlisting> - - <para> - The <literal>elements</literal> function accesses element set of the collection. - </para> - </sect1> - - <sect1 id="query-language-s5"> <title>The where clause</title> <para> ! The <literal>where</literal> clause allows you to narrow the list ! of instances returned. </para> ! <programlisting><![CDATA[from cat in class eg.Cat where cat.name='Fritz']]></programlisting> <para> --- 235,249 ---- (It also means you can't call these queries using <literal>Query.scroll()</literal>.) </para> + </sect1> <sect1 id="query-language-s5"> <title>The where clause</title> <para> ! The <literal>where</literal> clause allows you to narrow the list of instances returned. </para> ! <programlisting><![CDATA[from eg.Cat as cat where cat.name='Fritz']]></programlisting> <para> *************** *** 175,179 **** </para> ! <programlisting><![CDATA[select foo from foo in class eg.Foo, bar in class eg.Bar where foo.startDate = bar.date]]></programlisting> --- 251,255 ---- </para> ! <programlisting><![CDATA[select foo from eg.Foo foo, eg.Bar bar where foo.startDate = bar.date]]></programlisting> *************** *** 187,198 **** </para> ! <programlisting><![CDATA[from cat in class eg.Cat where cat.mate.name is not null]]></programlisting> <para> ! This query translates to an SQL query with a table join. If you were to write something like </para> ! <programlisting><![CDATA[from foo in class eg.Foo where foo.bar.baz.customer.address.city is not null]]></programlisting> --- 263,274 ---- </para> ! <programlisting><![CDATA[from eg.Cat cat where cat.mate.name is not null]]></programlisting> <para> ! This query translates to an SQL query with a table (inner) join. If you were to write something like </para> ! <programlisting><![CDATA[from eg.Foo foo where foo.bar.baz.customer.address.city is not null]]></programlisting> *************** *** 206,212 **** </para> ! <programlisting><![CDATA[from cat in class eg.Cat, rival in class eg.Cat where cat.mate = rival.mate ! select cat, mate from cat in class eg.Cat, mate in class eg.Cat where cat.mate = mate]]></programlisting> --- 282,288 ---- </para> ! <programlisting><![CDATA[from eg.Cat cat, eg.Cat rival where cat.mate = rival.mate ! select cat, mate from eg.Cat cat, eg.Cat mate where cat.mate = mate]]></programlisting> *************** *** 216,222 **** </para> ! <programlisting><![CDATA[from cat in class eg.Cat where cat.id = 123 ! from cat in class eg.Cat where cat.mate.id = 69]]></programlisting> <para> --- 292,298 ---- </para> ! <programlisting><![CDATA[from eg.Cat as cat where cat.id = 123 ! from eg.Cat as cat where cat.mate.id = 69]]></programlisting> <para> *************** *** 230,237 **** </para> ! <programlisting><![CDATA[from person in class bank.Person where person.id.country = 'AU' and person.id.medicareNumber = 123456 ! from account in class bank.Account where account.owner.id.country = 'AU' and account.owner.id.medicareNumber = 123456]]></programlisting> --- 306,313 ---- </para> ! <programlisting><![CDATA[from bank.Person person where person.id.country = 'AU' and person.id.medicareNumber = 123456 ! from bank.Account account where account.owner.id.country = 'AU' and account.owner.id.medicareNumber = 123456]]></programlisting> *************** *** 246,250 **** </para> ! <programlisting><![CDATA[from cat in class eg.Cat where cat.class = eg.DomesticCat]]></programlisting> <para> --- 322,326 ---- </para> ! <programlisting><![CDATA[from eg.Cat cat where cat.class = eg.DomesticCat]]></programlisting> <para> *************** *** 334,340 **** </para> ! <programlisting><![CDATA[from cat in class eg.DomesticCat where cat.name between 'A' and 'B' ! from cat in class eg.DomesticCat where cat.name in ( 'Foo', 'Bar', Baz" )]]></programlisting> <para> --- 410,416 ---- </para> ! <programlisting><![CDATA[from eg.DomesticCat cat where cat.name between 'A' and 'B' ! from eg.DomesticCat cat where cat.name in ( 'Foo', 'Bar', Baz" )]]></programlisting> <para> *************** *** 342,348 **** </para> ! <programlisting><![CDATA[from cat in class eg.DomesticCat where cat.name not between 'A' and 'B' ! from cat in class eg.DomesticCat where cat.name not in ( 'Foo', 'Bar', Baz" )]]></programlisting> <para> --- 418,424 ---- </para> ! <programlisting><![CDATA[from eg.DomesticCat cat where cat.name not between 'A' and 'B' ! from eg.DomesticCat cat where cat.name not in ( 'Foo', 'Bar', Baz" )]]></programlisting> <para> *************** *** 352,359 **** <para> ! You may test the size of a collection with the special property <literal>size</literal> </para> ! <programlisting><![CDATA[from cat in class eg.Cat where cat.kittens.size > 0]]></programlisting> <para> --- 428,438 ---- <para> ! You may test the size of a collection with the special property <literal>size</literal>, or ! the special <literal>size()</literal> function. </para> ! <programlisting><![CDATA[from eg.Cat cat where cat.kittens.size > 0 ! ! from eg.Cat cat where size(cat.kittens) > 0]]></programlisting> <para> *************** *** 364,368 **** </para> ! <programlisting><![CDATA[from cal in class Calendar where cal.holidays.maxElement > current date]]></programlisting> <para> --- 443,447 ---- </para> ! <programlisting><![CDATA[from Calendar cal where cal.holidays.maxElement > current date]]></programlisting> <para> *************** *** 370,376 **** </para> ! <programlisting><![CDATA[from order in class Order where maxindex(order.items) > 100 ! from order in class Order where size(order.items) > 100]]></programlisting> <para> --- 449,455 ---- </para> ! <programlisting><![CDATA[from Order order where maxindex(order.items) > 100 ! from Order order where minelement(order.items) > 10000]]></programlisting> <para> *************** *** 380,395 **** </para> ! <programlisting><![CDATA[select mother from mother in class eg.Cat, kit in class eg.Cat where kit in elements(foo.kittens) ! select p from list in class eg.NameList, p in class eg.Person where p.name = some elements(list.names) ! from cat in class eg.Cat where exists elements(cat.kittens) ! from p in class eg.Player where 3 > all elements(p.scores) ! from show in class eg.Show ! where 'fizard' in indices(show.acts)]]></programlisting> <para> --- 459,473 ---- </para> ! <programlisting><![CDATA[select mother from eg.Cat as mother, eg.Cat as kit where kit in elements(foo.kittens) ! select p from eg.NameList list, eg.Person p where p.name = some elements(list.names) ! from eg.Cat cat where exists elements(cat.kittens) ! from eg.Player p where 3 > all elements(p.scores) ! from eg.Show show where 'fizard' in indices(show.acts)]]></programlisting> <para> *************** *** 406,415 **** </para> </listitem> ! <listitem> <para> in a <literal>from</literal> clause: only <literal>elements</literal> makes sense ... and only for a collection of entities </para> ! </listitem> <listitem> <para> --- 484,493 ---- </para> </listitem> ! <!--<listitem> <para> in a <literal>from</literal> clause: only <literal>elements</literal> makes sense ... and only for a collection of entities </para> ! </listitem>--> <listitem> <para> *************** *** 424,437 **** </para> ! <programlisting><![CDATA[from order in class Order where order.items[0].id = 1234 ! select person from person in class Person, calendar in class Calendar where calendar.holidays['national day'] = person.birthDay and person.nationality.calendar = calendar ! select item from item in class Item, order in class Order where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11 ! select item from item in class Item, order in class Order where order.items[ maxindex(order.items) ] = item and order.id = 11]]></programlisting> --- 502,515 ---- </para> ! <programlisting><![CDATA[from Order order where order.items[0].id = 1234 ! select person from Person person, Calendar calendar where calendar.holidays['national day'] = person.birthDay and person.nationality.calendar = calendar ! select item from Item item, Order order where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11 ! select item from Item item, Order order where order.items[ maxindex(order.items) ] = item and order.id = 11]]></programlisting> *************** *** 440,444 **** </para> ! <programlisting><![CDATA[select item from item in class Item, order in class Order where order.items[ size(order.items) - 1 ] = item]]></programlisting> --- 518,522 ---- </para> ! <programlisting><![CDATA[select item from Item item, Order order where order.items[ size(order.items) - 1 ] = item]]></programlisting> *************** *** 447,451 **** </para> ! <programlisting><![CDATA[from cat in class eg.DomesticCat where upper(cat.name) like 'FRI%']]></programlisting> <para> --- 525,529 ---- </para> ! <programlisting><![CDATA[from eg.DomesticCat cat where upper(cat.name) like 'FRI%']]></programlisting> <para> *************** *** 455,461 **** <programlisting><![CDATA[select cust ! from prod in class Product, ! store in class Store, ! cust in store.customers where prod.name = 'widget' and store.location.name in ( 'Melbourne', 'Sydney' ) --- 533,539 ---- <programlisting><![CDATA[select cust ! from Product prod, ! Store store ! inner join store.customers cust where prod.name = 'widget' and store.location.name in ( 'Melbourne', 'Sydney' ) *************** *** 494,498 **** </para> ! <programlisting><![CDATA[from cat in class eg.DomesticCat order by cat.name asc, cat.weight desc, cat.birthdate]]></programlisting> --- 572,576 ---- </para> ! <programlisting><![CDATA[from eg.DomesticCat cat order by cat.name asc, cat.weight desc, cat.birthdate]]></programlisting> *************** *** 511,518 **** </para> ! <programlisting><![CDATA[select cat.color, sum(cat.weight), count(cat) from cat in class eg.Cat group by cat.color ! select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from foo in class eg.Foo group by foo.id]]></programlisting> --- 589,596 ---- </para> ! <programlisting><![CDATA[select cat.color, sum(cat.weight), count(cat) from eg.Cat cat group by cat.color ! select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id]]></programlisting> *************** *** 522,526 **** <para>A <literal>having</literal> clause is also allowed.</para> ! <programlisting><![CDATA[select cat.color, sum(cat.weight), count(cat) from foo in class eg.Foo group by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)]]></programlisting> --- 600,604 ---- <para>A <literal>having</literal> clause is also allowed.</para> ! <programlisting><![CDATA[select cat.color, sum(cat.weight), count(cat) from eg.Cat cat group by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK)]]></programlisting> *************** *** 536,547 **** </para> ! <programlisting><![CDATA[from fatcat in class eg.Cat where fatcat.weight > ! ( select avg(cat.weight) from cat in class eg.DomesticCat ) ! from cat in class eg.DomesticCat where cat.name = ! some( select list.name from list in class eg.NameList ) ! from cat in class eg.Cat where not exists ! ( from mate in class eg.Cat where mate.mate = cat )]]></programlisting> </sect1> --- 614,625 ---- </para> ! <programlisting><![CDATA[from eg.Cat fatcat where fatcat.weight > ! ( select avg(cat.weight) from eg.DomesticCat cat ) ! from eg.DomesticCat cat where cat.name = ! some( select list.name from eg.NameList list ) ! from eg.Cat cat where not exists ! ( from eg.Cat mate where mate.mate = cat )]]></programlisting> </sect1> |