Hibernate's retrieval strategies include class-level retrieval policies and association-level retrieval strategies.
The class-level retrieval policy has immediate and deferred retrieval, and the default retrieval policy is immediate retrieval. In the hibernate mapping file, the retrieval policy is determined by configuring the Lazy property on the < class>. For the retrieval of session, class-level retrieval strategy only applies to the Load method; it says, for get, Qurey retrieval, The persisted object is immediately loaded regardless of whether lazy is false or true. In general, we retrieve objects to access it, so immediate retrieval is the usual choice. Because the Load method throws an exception when the object is not retrieved (in the case of immediate retrieval), I personally do not recommend load retrieval, and since the lazy attribute in < class> also affects a many-to-many and one-to-one retrieval strategy, it is even more unnecessary to use the Load method.
The association level retrieval policy has immediate retrieval, delayed retrieval, and urgent left-outer connection retrieval. For relevance-level retrieval, it can be divided into One-to-many and many-to-many, One-to-many pairs and one or two kinds of situations.
One-to-many and Many-to-many Association relationships are generally used < set> configurations. < set> have lazy and outer-join properties, and their different values are absolutely retrieval strategies.
1) Search Now: This is a one-to-many default retrieval strategy, which is lazy=false,outer-join=false at this time. Although this is the default retrieval policy, do not use this method if the associated collection is useless.
2 Delay Search: At this time Lazy=true,outer-join=false (Outer-join=true is meaningless), this is the preferred way of retrieval.
3 Urgent LEFT Outer connection search: At this point in time lazy=false,outer-join=true, this retrieval strategy applies only to the retrieval of IDs (load, get) and not to query collection (which takes the immediate retrieval policy). This retrieval strategy reduces a single SQL statement compared to immediate retrieval, but in Hibernate, only one is configured to Outer-join=true.
Multi-pair and one-to-one retrieval strategies typically use < Many-to-one>, < one-to-one> configurations. The properties that need to be configured in < many-to-one> are outer-join, and you also need to configure the lazy properties of the one-side associated < class> (not configured many-to-one> in < lazy), Their combination of retrieval strategies is as follows:
1 Outer-join=auto: This is the default value, if lazy=true for deferred retrieval, if Lazy=false is an urgent left outer join retrieval.
2 outer-join=true, no about lazy, are urgent to the left outer connection search.
3) Outer-join=false, if Lazy=true is a deferred search, it is retrieved immediately.
You can see that, by default (Outer-join=auto,lazy=false), an urgent left-side connection is retrieved for the associated one-object hibernate. In my opinion, in many cases, we do not need to load the object associated with one end (it is likely that we need only the ID of the associated object), and if the associated object also uses an urgent left outer join, there will be multiple outer join tables in the SELECT statement, if the number of words will affect the retrieval performance, This is why hibernate controls the depth of the outer joins through the Hibernate.max_fetch_depth property. For an urgent left OUTER join search, query collection retrieval does not apply, and it takes immediate retrieval policy.
For the retrieval strategy, it is necessary to choose according to the actual situation. For immediate and deferred retrieval, the advantage is that the SELECT statement is simple (one statement per table), the query is fast, and the disadvantage is that the associated table requires multiple SELECT statements, increasing the frequency of access to the database. Therefore, you can consider using a bulk retrieval policy to reduce the number of SELECT statements (Configure the Batch-size property) when choosing to retrieve and defer retrieval. For Chezo external connection retrieval, the advantage is that there are fewer select, but the disadvantage is that the complexity of the SELECT statement increases, 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 can be retrieved according to the specified retrieval policy as needed in the program (which may often need to be shown in the program to indicate an urgent left-outer connection). To understand how the configuration effects of a retrieval policy are configured, you can configure the Show_sql property to view the SQL statements that are executed hibernate the program runs.