Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Hibernate query statements?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what Hibernate query sentences, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. Select clause

The select clause selects which objects and attributes are returned to the query result set. Consider the following:

Select mate from Cat as cat inner join cat.mate as mate

This statement will select mates of other Cats. (spouses of other cats) in fact, you can express the same meaning more succinctly with the following query:

Select cat.mate from Cat cat

Hibernate query statements can return properties of any type, including those of a component (Component):

Select cat.name from DomesticCat cat where cat.name like 'fri%' select cust.name.firstName from Customer as cust

Hibernate query statements can return multiple objects and / or attributes and store them in the Object [] queue

Select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr

Or stored in a List object

Select new list (mother, offspr, mate.name) from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr

It is also possible to return an actual type-safe Java object directly

Select new Family (mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr

Suppose the class Family has an appropriate constructor.

You can use the keyword as to assign an alias to the selected expression:

Select max (bodyWeight) as max, min (bodyWeight) as min, count (*) as n from Cat cat

This is most useful when used with the clause select new map:

Select new map (max (bodyWeight) as max, min (bodyWeight) as min, count (*) as n) from Cat cat

The Hibernate query returns an object of Map with a name-value mapping of the alias and the selected value.

3. Aggregate function

A HQL query can even return the calculated results of an aggregate function that acts on an attribute:

Select avg (cat.weight), sum (cat.weight), max (cat.weight), count (cat) from Cat cat

The supported aggregation functions are as follows:

Avg (...), sum (...), min (...), max (...) Count (*) count (...), count (distinct...), count (all...)

You can use mathematical operators, joins, and validated SQL functions in the selection clause:

Select cat.weight + sum (kitten.weight) from Cat cat join cat.kittens kitten group by cat.id, cat.weight select firstName | |''| initial | |''| | upper (lastName) from Person

The keywords distinct and all can also be used, which have the same semantics as SQL.

Select distinct cat.name from Cat cat select count (distinct cat.name), count (cat) from Cat cat

3. Polymorphic query

A Hibernate query statement like this:

From Cat as cat

Returns not only an instance of the Cat class, but also an instance of the subclass DomesticCat. Hibernate can specify any Java class or interface in the from clause. The query returns instances of all persistent subclasses that inherit the class or instances of all persistent classes that declare the interface. The following query returns all persisted objects:

From java.lang.Object o

The interface Named may be declared by a variety of persistence classes:

From Named n, Named m where n.name = m.name

Note: two queries of * * will require more than one SQL SELECT. This indicates that the order by clause does not sort the entire result set correctly. This also means that you cannot use the Query.scroll () method on such a query.

The above is all the contents of the article "what are the Hibernate query sentences?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 279

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report