Multi table query and fetching strategy in Hibernate
- 2020-06-12 09:01:11
- OfStack
1. Multi-table queries in Hibernate
1.1 Multi-table queries in SQL
[Cross connection]
select * from A,B;
[Internal connection]
Display inner connection :inner join(inner can be omitted)
Select * from inner join B on conditions;
Implicit internal connection:
Select * from A,B where conditions;
[External connection]
Left outer connection: left outer join
Select * from A left join B on conditions;
Outer right connection: right outer join
Select * from A right join B on conditions;
1.2 Multi-table join queries in Hibernate
[Cross connection]
Cross connection
[Internal connection]
Displays the internal connection from Customer c inner join ES83en.linkmans
Implicit inner connection
Urgent internal connection from Customer c join fetch c.linkmans
[External connection]
The left outer join
Right connection
Pressing left outer connection
2. Grab strategy in Hibernate
2.1 Lazy loading of lazy
lazy lazy loading: sql statement is not sent when querying and sql statement is sent when using objects
Lazy load Lazy load of atmosphere class level and lazy load of association level
2.1.1 Class level lazy loading
The delay in querying a class using lazy-loaded methods is called a class-level delay. The default is true.
Customer customer = session.load(Customer.class,1l);// 默认就会采用延迟加载,这种称为是类级别的延迟。
Class level lazy loading failure:
* final decorates this class so that proxy classes cannot be generated and lazy loading will fail.
* in < class > Configure lazy false = ""
2.1.2 Lazy loading at the association level
When an object is queried, its associated object is obtained. The delay in querying its associated objects. Is the latency at the association level.
Customer c = session.get(Customer.class,1l);
c.getLinkMans(); // Whether to use lazy loading when querying associated objects.
Latency at the association level is often used with fetching strategy 1 to optimize the program. The latency at the association level is at < set > Or is it < many-to-one > Lazy loading on labels)
2.2 Fetching strategy
Fetching strategy refers to the strategy used to fetch the associated objects after finding an object. The fetching strategy is based on the configuration of the associated object ( < set > and < many-to-one > ) configure the fetch attribute.
2.2.1 lazy and fetch configured on set
fetch: Grab policy that controls the format of SQL statements sent.
* select: Default. Send an select statement to query the associated object.
* join: Send 1 urgent left outer join query associated object.
* subselect: Send 1 sub-query to query the associated object.
lazy: Lazy loading controls when SQL statements are sent.
* true: Default. Lazy loading is used.
* false: No lazy loading.
extra: Extremely lazy.
2.2.2 lazy and fetch configured on ES196en-ES197en-ES198en
fetch: Fetching strategy that controls the format of SQL statements sent.
* select: Default. Send 1 select statement to query the associated object.
* join: Send 1 urgent left outer join query associated object.
lazy: Lazy loading controls when SQL is sent.
* proxy: Default. It is up to the lazy loading on the other class to decide whether to adopt a delay or not.
* false: No lazy loading.
* no - proxy:
2.2.3 Batch fetching
Bulk fetching: When querying multiple customers, querying all contacts under multiple customers.
In Customer. hbm. xml < set > Configure n batch - size = ""