📄 query_hql.pot
字号:
msgstr ""#. Tag: programlisting#: query_hql.xml:861#, no-c-formatmsgid "" "<![CDATA[select cat.id, (select max(kit.weight) from cat.kitten kit)\n" "from Cat as cat]]>"msgstr ""#. Tag: para#: query_hql.xml:863#, no-c-formatmsgid "Note that HQL subqueries may occur only in the select or where clauses."msgstr ""#. Tag: para#: query_hql.xml:867#, no-c-formatmsgid "Note that subqueries can also utilize <literal>row value constructor</literal> syntax. See <xref linkend=\"queryhql-tuple\"/> for more details."msgstr ""#. Tag: title#: query_hql.xml:875#, no-c-formatmsgid "HQL examples"msgstr ""#. Tag: para#: query_hql.xml:877#, no-c-formatmsgid "Hibernate queries can be quite powerful and complex. In fact, the power of the query language is one of Hibernate's main selling points. Here are some example queries very similar to queries that I used on a recent project. Note that most queries you will write are much simpler than these!"msgstr ""#. Tag: para#: query_hql.xml:883#, no-c-formatmsgid "The following query returns the order id, number of items and total value of the order for all unpaid orders for a particular customer and given minimum total value, ordering the results by total value. In determining the prices, it uses the current catalog. The resulting SQL query, against the <literal>ORDER</literal>, <literal>ORDER_LINE</literal>, <literal>PRODUCT</literal>, <literal>CATALOG</literal> and <literal>PRICE</literal> tables has four inner joins and an (uncorrelated) subselect."msgstr ""#. Tag: programlisting#: query_hql.xml:892#, no-c-formatmsgid "" "<![CDATA[select order.id, sum(price.amount), count(item)\n" "from Order as order\n" " join order.lineItems as item\n" " join item.product as product,\n" " Catalog as catalog\n" " join catalog.prices as price\n" "where order.paid = false\n" " and order.customer = :customer\n" " and price.product = product\n" " and catalog.effectiveDate < sysdate\n" " and catalog.effectiveDate >= all (\n" " select cat.effectiveDate\n" " from Catalog as cat\n" " where cat.effectiveDate < sysdate\n" " )\n" "group by order\n" "having sum(price.amount) > :minAmount\n" "order by sum(price.amount) desc]]>"msgstr ""#. Tag: para#: query_hql.xml:894#, no-c-formatmsgid "What a monster! Actually, in real life, I'm not very keen on subqueries, so my query was really more like this:"msgstr ""#. Tag: programlisting#: query_hql.xml:899#, no-c-formatmsgid "" "<![CDATA[select order.id, sum(price.amount), count(item)\n" "from Order as order\n" " join order.lineItems as item\n" " join item.product as product,\n" " Catalog as catalog\n" " join catalog.prices as price\n" "where order.paid = false\n" " and order.customer = :customer\n" " and price.product = product\n" " and catalog = :currentCatalog\n" "group by order\n" "having sum(price.amount) > :minAmount\n" "order by sum(price.amount) desc]]>"msgstr ""#. Tag: para#: query_hql.xml:901#, no-c-formatmsgid "The next query counts the number of payments in each status, excluding all payments in the <literal>AWAITING_APPROVAL</literal> status where the most recent status change was made by the current user. It translates to an SQL query with two inner joins and a correlated subselect against the <literal>PAYMENT</literal>, <literal>PAYMENT_STATUS</literal> and <literal>PAYMENT_STATUS_CHANGE</literal> tables."msgstr ""#. Tag: programlisting#: query_hql.xml:909#, no-c-formatmsgid "" "<![CDATA[select count(payment), status.name\n" "from Payment as payment\n" " join payment.currentStatus as status\n" " join payment.statusChanges as statusChange\n" "where payment.status.name <> PaymentStatus.AWAITING_APPROVAL\n" " or (\n" " statusChange.timeStamp = (\n" " select max(change.timeStamp)\n" " from PaymentStatusChange change\n" " where change.payment = payment\n" " )\n" " and statusChange.user <> :currentUser\n" " )\n" "group by status.name, status.sortOrder\n" "order by status.sortOrder]]>"msgstr ""#. Tag: para#: query_hql.xml:911#, no-c-formatmsgid "If I would have mapped the <literal>statusChanges</literal> collection as a list, instead of a set, the query would have been much simpler to write."msgstr ""#. Tag: programlisting#: query_hql.xml:916#, no-c-formatmsgid "" "<![CDATA[select count(payment), status.name\n" "from Payment as payment\n" " join payment.currentStatus as status\n" "where payment.status.name <> PaymentStatus.AWAITING_APPROVAL\n" " or payment.statusChanges[ maxIndex(payment.statusChanges) ].user <> :currentUser\n" "group by status.name, status.sortOrder\n" "order by status.sortOrder]]>"msgstr ""#. Tag: para#: query_hql.xml:918#, no-c-formatmsgid "The next query uses the MS SQL Server <literal>isNull()</literal> function to return all the accounts and unpaid payments for the organization to which the current user belongs. It translates to an SQL query with three inner joins, an outer join and a subselect against the <literal>ACCOUNT</literal>, <literal>PAYMENT</literal>, <literal>PAYMENT_STATUS</literal>, <literal>ACCOUNT_TYPE</literal>, <literal>ORGANIZATION</literal> and <literal>ORG_USER</literal> tables."msgstr ""#. Tag: programlisting#: query_hql.xml:927#, no-c-formatmsgid "" "<![CDATA[select account, payment\n" "from Account as account\n" " left outer join account.payments as payment\n" "where :currentUser in elements(account.holder.users)\n" " and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)\n" "order by account.type.sortOrder, account.accountNumber, payment.dueDate]]>"msgstr ""#. Tag: para#: query_hql.xml:929#, no-c-formatmsgid "For some databases, we would need to do away with the (correlated) subselect."msgstr ""#. Tag: programlisting#: query_hql.xml:933#, no-c-formatmsgid "" "<![CDATA[select account, payment\n" "from Account as account\n" " join account.holder.users as user\n" " left outer join account.payments as payment\n" "where :currentUser = user\n" " and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)\n" "order by account.type.sortOrder, account.accountNumber, payment.dueDate]]>"msgstr ""#. Tag: title#: query_hql.xml:938#, no-c-formatmsgid "Bulk update and delete"msgstr ""#. Tag: para#: query_hql.xml:940#, no-c-formatmsgid "HQL now supports <literal>update</literal>, <literal>delete</literal> and <literal>insert ... select ...</literal> statements. See <xref linkend=\"batch-direct\"/> for details."msgstr ""#. Tag: title#: query_hql.xml:948#, no-c-formatmsgid "Tips & Tricks"msgstr ""#. Tag: para#: query_hql.xml:950#, no-c-formatmsgid "You can count the number of query results without actually returning them:"msgstr ""#. Tag: programlisting#: query_hql.xml:954#, no-c-formatmsgid "<![CDATA[( (Integer) session.createQuery(\"select count(*) from ....\").iterate().next() ).intValue()]]>"msgstr ""#. Tag: para#: query_hql.xml:956#, no-c-formatmsgid "To order a result by the size of a collection, use the following query:"msgstr ""#. Tag: programlisting#: query_hql.xml:960#, no-c-formatmsgid "" "<![CDATA[select usr.id, usr.name\n" "from User as usr\n" " left join usr.messages as msg\n" "group by usr.id, usr.name\n" "order by count(msg)]]>"msgstr ""#. Tag: para#: query_hql.xml:962#, no-c-formatmsgid "If your database supports subselects, you can place a condition upon selection size in the where clause of your query:"msgstr ""#. Tag: programlisting#: query_hql.xml:967#, no-c-formatmsgid "<![CDATA[from User usr where size(usr.messages) >= 1]]>"msgstr ""#. Tag: para#: query_hql.xml:969#, no-c-formatmsgid "If your database doesn't support subselects, use the following query:"msgstr ""#. Tag: programlisting#: query_hql.xml:973#, no-c-formatmsgid "" "<![CDATA[select usr.id, usr.name\n" "from User usr.name\n" " join usr.messages msg\n" "group by usr.id, usr.name\n" "having count(msg) >= 1]]>"msgstr ""#. Tag: para#: query_hql.xml:975#, no-c-formatmsgid "As this solution can't return a <literal>User</literal> with zero messages because of the inner join, the following form is also useful:"msgstr ""#. Tag: programlisting#: query_hql.xml:980#, no-c-formatmsgid "" "<![CDATA[select usr.id, usr.name\n" "from User as usr\n" " left join usr.messages as msg\n" "group by usr.id, usr.name\n" "having count(msg) = 0]]>"msgstr ""#. Tag: para#: query_hql.xml:982#, no-c-formatmsgid "Properties of a JavaBean can be bound to named query parameters:"msgstr ""#. Tag: programlisting#: query_hql.xml:986#, no-c-formatmsgid "" "<![CDATA[Query q = s.createQuery(\"from foo Foo as foo where foo.name=:name and foo.size=:size\");\n" "q.setProperties(fooBean); // fooBean has getName() and getSize()\n" "List foos = q.list();]]>"msgstr ""#. Tag: para#: query_hql.xml:988#, no-c-formatmsgid "Collections are pageable by using the <literal>Query</literal> interface with a filter:"msgstr ""#. Tag: programlisting#: query_hql.xml:992#, no-c-formatmsgid "" "<![CDATA[Query q = s.createFilter( collection, \"\" ); // the trivial filter\n" "q.setMaxResults(PAGE_SIZE);\n" "q.setFirstResult(PAGE_SIZE * pageNumber);\n" "List page = q.list();]]>"msgstr ""#. Tag: para#: query_hql.xml:994#, no-c-formatmsgid "Collection elements may be ordered or grouped using a query filter:"msgstr ""#. Tag: programlisting#: query_hql.xml:998#, no-c-formatmsgid "" "<![CDATA[Collection orderedCollection = s.filter( collection, \"order by this.amount\" );\n" "Collection counts = s.filter( collection, \"select this.type, count(this) group by this.type\" );]]>"msgstr ""#. Tag: para#: query_hql.xml:1000#, no-c-formatmsgid "You can find the size of a collection without initializing it:"msgstr ""#. Tag: programlisting#: query_hql.xml:1004#, no-c-formatmsgid "<![CDATA[( (Integer) session.createQuery(\"select count(*) from ....\").iterate().next() ).intValue();]]>"msgstr ""#. Tag: title#: query_hql.xml:1009#, no-c-formatmsgid "Components"msgstr ""#. Tag: para#: query_hql.xml:1011#, no-c-formatmsgid "Components might be used in just about every way that simple value types can be used in HQL queries. They can appear in the <literal>select</literal> clause:"msgstr ""#. Tag: programlisting#: query_hql.xml:1016 query_hql.xml:1062#, no-c-formatmsgid "<![CDATA[select p.name from Person p]]>"msgstr ""#. Tag: programlisting#: query_hql.xml:1017#, no-c-formatmsgid "<![CDATA[select p.name.first from Person p]]>"msgstr ""#. Tag: para#: query_hql.xml:1019#, no-c-formatmsgid "where the Person's name property is a component. Components can also be used in the <literal>where</literal> clause:"msgstr ""#. Tag: programlisting#: query_hql.xml:1024#, no-c-formatmsgid "<![CDATA[from Person p where p.name = :name]]>"msgstr ""#. Tag: programlisting#: query_hql.xml:1025#, no-c-formatmsgid "<![CDATA[from Person p where p.name.first = :firstName]]>"msgstr ""#. Tag: para#: query_hql.xml:1027#, no-c-formatmsgid "Components can also be used in the <literal>order by</literal> clause:"msgstr ""#. Tag: programlisting#: query_hql.xml:1031#, no-c-formatmsgid "<![CDATA[from Person p order by p.name]]>"msgstr ""#. Tag: programlisting#: query_hql.xml:1032#, no-c-formatmsgid "<![CDATA[from Person p order by p.name.first]]>"msgstr ""#. Tag: para#: query_hql.xml:1034#, no-c-formatmsgid "Another common use of components is in <link linkend=\"queryhql-tuple\">row value constructors</link>."msgstr ""#. Tag: title#: query_hql.xml:1040#, no-c-formatmsgid "Row value constructor syntax"msgstr ""#. Tag: para#: query_hql.xml:1042#, no-c-formatmsgid "HQL supports the use of ANSI SQL <literal>row value constructor</literal> syntax (sometimes called <literal>tuple</literal> syntax), even though the underlying database may not support that notion. Here we are generally referring to multi-valued comparisons, typically associated with components. Consider an entity Person which defines a name component:"msgstr ""#. Tag: programlisting#: query_hql.xml:1049#, no-c-formatmsgid "<![CDATA[from Person p where p.name.first='John' and p.name.last='Jingleheimer-Schmidt']]>"msgstr ""#. Tag: para#: query_hql.xml:1051#, no-c-formatmsgid "That's valid syntax, although a little verbose. It be nice to make this a bit more concise and use <literal>row value constructor</literal> syntax:"msgstr ""#. Tag: programlisting#: query_hql.xml:1056#, no-c-formatmsgid "<![CDATA[from Person p where p.name=('John', 'Jingleheimer-Schmidt')]]>"msgstr ""#. Tag: para#: query_hql.xml:1058#, no-c-formatmsgid "It can also be useful to specify this in the <literal>select</literal> clause:"msgstr ""#. Tag: para#: query_hql.xml:1064#, no-c-formatmsgid "Another time using <literal>row value constructor</literal> syntax can be beneficial is when using subqueries needing to compare against multiple values:"msgstr ""#. Tag: programlisting#: query_hql.xml:1069#, no-c-formatmsgid "" "<![CDATA[from Cat as cat\n" "where not ( cat.name, cat.color ) in (\n" " select cat.name, cat.color from DomesticCat cat\n" ")]]>"msgstr ""#. Tag: para#: query_hql.xml:1071#, no-c-formatmsgid "One thing to consider when deciding if you want to use this syntax is that the query will be dependent upon the ordering of the component sub-properties in the metadata."msgstr ""
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -