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 is the Hibernate retrieval strategy?

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

Share

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

This article introduces the relevant knowledge of "what is the Hibernate retrieval strategy". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Hibernate retrieval strategy includes class-level retrieval strategy and association-level retrieval strategy.

Class-level retrieval strategies include immediate retrieval and delayed retrieval, and the default retrieval strategy is immediate retrieval. "in the Hibernate mapping file, determine the retrieval policy by configuring the lazy property on the." For Session retrieval, the class-level retrieval strategy is only applicable to load methods; that is, for get and qurey retrieval, persistent objects are loaded immediately, regardless of whether lazy is false or true. Generally speaking, the object we retrieve is to access it, so immediate retrieval is the common choice. Because the load method throws an exception when the object cannot be retrieved (in the case of immediate retrieval., I personally do not recommend using load retrieval; and because the lazy attribute in also affects many-to-one and one-to-one retrieval strategies, it is even less necessary to use the load method.

The retrieval strategies of association level include immediate retrieval, delayed retrieval and urgent left outer link retrieval. For association-level retrieval, it can be divided into one-to-many and many-to-many, many-to-one and one-to-one situations.

One-to-many and many-to-many associations generally use configurations. There are lazy and outer-join attributes, and their different values absolutely determine the retrieval strategy.

1. Search immediately: this is the one-to-many default search strategy, where lazy=false,outer-join=false. Although this is the default retrieval strategy, do not use this retrieval method if the associated collection is useless.

two。 Delayed retrieval: lazy=true,outer-join=false (outer-join=true is meaningless at this time. This is the preferred retrieval method.

3. Urgent left outer link retrieval: at this time lazy=false,outer-join=true, this retrieval strategy is only suitable for collection retrieval that relies on id retrieval (load, get.,) but not query (it will adopt the immediate retrieval strategy. Compared to immediate retrieval, this retrieval strategy reduces one sql statement, but in Hibernate, only one can be configured as outer-join=true.

Many-to-one and one-to-one retrieval strategies are generally used and configured. The attribute that needs to be configured in is outer-join. At the same time, you also need to configure the lazy attribute associated with the one side (not the lazy in the configuration. The retrieval strategy after their combination is as follows:

1.outer-join=auto: this is the default if lazy=true is delayed retrieval, if lazy=false is urgent left outer link retrieval.

2.outer-join=true: nothing about lazy, all for urgent left outer link search.

3.outer-join=false: if lazy=true is delayed retrieval, otherwise it is immediate retrieval.

As you can see, by default, outer-join=auto,lazy=false., uses urgent left outer connection retrieval for the associated one-side object Hibernate. In my opinion, in many cases, we do not need to load the associated objects on the one side (it is very likely that all we need is the id.; of the associated objects. In addition, if the associated objects also use urgent left outer join retrieval, there will be multiple external join tables in the select statement, which is why Hibernate controls the depth of the outer join through the hibernate.max_fetch_depth attribute. For urgent left outer join retrieval, query's set retrieval is not applicable, it will adopt the immediate retrieval strategy.

For Hibernate retrieval strategy, we need to choose it according to the actual situation. For immediate retrieval and delayed retrieval, their advantages are that the select statement is simple (one statement per table), the query speed is fast, and the disadvantage is that multiple select statements are needed to associate tables, which increases the frequency of accessing the database. Therefore, when choosing to retrieve or delay retrieval, you can consider using a batch retrieval strategy to reduce the number of select statements (configuring the batch-size attribute). For the left outer join retrieval, the advantage is that there is less select, but the disadvantage is that the complexity of the select statement is increased, and the association between multiple tables can be a time-consuming operation. In addition, the configuration file is dead, but the program is alive, and you can specify the retrieval strategy that needs to be displayed in the program (you may often need to display the specified urgent left outer link search in the program). To understand how the retrieval policy is configured, you can configure the show_sql property to view the sql statements executed by Hibernate when the program is running.

This is the end of the content of "what is the Hibernate Retrieval Strategy". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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