A model class that performs multiple requests in sleep mode ..!

When I use sleep mode matching using

@OneToOne @oneToMany @ManyToOne 

Associates its creation of several requests for each selection.

Can someone help me understand how relationships work. ??

+4
source share
1 answer

Sleep mode works with different sampling strategies .. !!

Hibernate provides 4 strategies for retrieving data:

SELECT

 @OneToMany(mappedBy="tableName", cascade=CascadeType.ALL) @Column(name="id") @Fetch(FetchMode.SELECT) 

This method runs several SQL queries. This first one is fired to retrieve all the records in the parent table. The remaining recording entries for each parent entry. This is basically an N + 1 problem. The first query retrieves N records from the database, in this case N parent records. For each parent, a new Child query is returned. Therefore, for N parent queries, N queries retrieve information from the children table.

Join

 @OneToMany(mappedBy="tableName", cascade=CascadeType.ALL) @Column(name="id") @Fetch(FetchMode.JOIN) 

This is similar to a SELECT sampling strategy, with the exception of the fact that all database extraction happens beforehand in a JOIN fetch, unlike SELECT where it happens as needed. This can become an important efficiency.

subselection

  @OneToMany(mappedBy="tableName", cascade=CascadeType.ALL) @Column(name="id") @Fetch(FetchMode.SUBSELECT) 

Two SQL are running. One to retrieve all the parents, and the second SUBSELECT in the WHERE clause to retrieve the entire child, which has a matching identifier for the parent.

PARTIES

 @OneToMany(mappedBy="tableName", cascade=CascadeType.ALL) @Column(name="id") @@BatchSize(size=2) 

The batch size is compared with the number of the parent whose child is being removed. Thus, we can specify the number of records to be selected at a time. Several requests will be executed. !!

one-to-many and many-to-many permits - join, Select and SubSelect

many-to-one and one-to-one allows - Join and Select


Hibernate also distinguishes (when associations are selected)

1. Direct sampling -

the relationship, collection, or attribute is retrieved immediately when the Parent is loaded. (Lazy = "false")

2. Lazy collection fetching -

the collection is retrieved when the application invokes the operation of this collection. (This is the default value for collections. (Lazy = "true")

3. Extra-lazy "collection choice -

Access to individual elements of the collection is carried out from the database as needed. Hibernate tries not to collect the entire collection in memory if it is absolutely necessary (suitable for very large collections) (Lazy = "extra")

4. Getting a proxy server -

unambiguous association is retrieved when a method other than the getter identifier is invoked on the associated object. (Lazy = "proxy")

5. Without a proxy server "-

An unambiguous association is selected when an instance variable is accessed. Compared to fetching proxies, this approach is less lazy. (Lazy = "no-proxy")

6. Lazy attribute selection -

an attribute or unambiguous relationship is retrieved when an instance accesses a variable. (Lazy = "truth")

one-to-many and many-to-many allows Immediate, Layzy, Extra Lazy

many-to-one and one-to-one allows Immediate Proxy, No Proxy

+13
source

Source: https://habr.com/ru/post/922307/


All Articles