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

How to use Hibernate query language

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Hibernate query language". In daily operation, I believe many people have doubts about how to use Hibernate query language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Hibernate query language". Next, please follow the editor to study!

Hibernate comes with a very powerful Hibernate query language that looks a lot like SQL. This paper mainly introduces case sensitivity, from clause, Association and Join and so on. But don't be fooled by grammatical structural similarities. HQL is consciously designed as a fully object-oriented query that can understand concepts such as inheritance, polymorphism, and association.

1. Case sensitivity problem

Query statements are not case-sensitive except for the names of Java classes and attributes. So SeLeCT is the same as sELEct and SELECT, but org.hibernate.eg.FOO is not equivalent to org.hibernate.eg.Foo and foo.barSet is not equivalent to foo.BARSET.

The HQL keywords in this manual will be in lowercase letters. Many users find that using completely uppercase keywords makes query statements more readable, but we find it ugly to use uppercase keywords when embedding query statements in Java statements.

2. From clause

The simplest form of Hibernate query statement is as follows:

From eg.Cat

This clause simply returns all instances of the eg.Cat class. Usually we don't need to use the fully qualified name of the class because auto-import (automatic introduction) is the default. So we almost only use the following simple writing:

From Cat

In most cases, you need to specify an alias because you may need to refer to Cat in other parts of the Hibernate query language

Om Cat as cat

This statement assigns the alias cat to an instance of class Cat so that we can use this alias in subsequent queries. The keyword as is optional, and we can also write:

From Cat cat

Multiple classes can appear in a clause at the same time, and the query result is to produce a Cartesian product or a join across tables.

From Formula, Parameter from Formula as form, Parameter as param

Lowercase at the beginning of aliases in query statements is considered a good practice, which is consistent with the naming standard of Java variables (for example, domesticCat).

3. Association (Association) and connection (Join)

Hibernate query language, we can also specify an alias for associated entities or even for all elements in a collection, using the keyword join.

From Cat as cat inner join cat.mate as mate left outer join cat.kittens as kitten from Cat as cat left join cat.mate.kittens as kittens from Formula form full join form.parameter param

The supported connection types are borrowed from ANSI SQL.

Inner join (Internal connection)

Left outer join (left outer link)

Right outer join (right external connection)

Full join (fully connected, not commonly used)

The statements inner join, left outer join and right outer join can be abbreviated.

From Cat as cat join cat.mate as mate left join cat.kittens as kitten

Also, a "fetch" connection allows associated objects or sets of values to be initialized with their parent object initialization using only a select statement, which is especially useful when using collections, which effectively replaces outer joins and deferred declarations (lazy declarations) in mapping files. See Section 19.1, "crawling Strategy (Fetching strategies)" for more information.

From Cat as cat inner join fetch cat.mate left join fetch cat.kittens

An fetch connection usually does not need to be assigned an alias because the associated object should not be used in the where clause (or any other clause). At the same time, associated objects are not returned directly in the results of the query, but they can be accessed through their parent object.

Note that in the current version, only one collection role can be concatenated in a query (more than one role will result in a Cartesian product). Also note that the fetch constructor cannot be used in queries that use the scroll () or iterate () functions. * Note that there is no point in using full join fetch with right join fetch.

If you use attribute-level deferred acquisition (lazy fetching) (this is achieved by rewriting bytecode), you can use fetch all properties to force Hibernate to get those attributes that would otherwise be delayed loading immediately (in * queries).

From Document fetch all properties order by name from Document doc fetch all properties where lower (doc.name) like'% cats%' here, the study on "how to use Hibernate query language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 0

*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