English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Multi-table Query and Fetching Strategy in Hibernate

1.Hibernate multi-table query

 1.1SQL multi-table query

[Cross join]

select * from A, B;

[Inner join]

Explicit inner join: inner join (inner can be omitted)

Select * from A inner join B on condition;

Implicit inner join:

Select * from A, B where condition;

[Outer join]

Left outer join: left outer join

Select * from A left outer join B on condition;

Right outer join: right outer join

Select * from A right outer join B on condition;

1.2Hibernate's multi-table join query

[Cross join]

Cross join

[Inner join]

Explicit inner join from Customer c inner join c.linkmans

Implicit inner join

Eager inner join from Customer c inner join fetch c.linkmans

[Outer join]

Left outer join

Right outer join

Eager left outer join

2.Hibernate's crawling strategy

2.1 Lazy loading lazy

Lazy loading: Do not send SQL statements when querying, and send SQL statements to query when using objects

Lazy loading includes class level lazy loading and association level lazy loading

2.1.1Class level lazy loading

When querying a class using lazy loading, whether to use lazy loading is called class level delay. The default value is true.

Customer customer = session.load(Customer.class,1l);// Lazy loading is used by default, which is called class level delay. 

Class level lazy loading fails:

* The final keyword modifies this class, cannot generate proxy classes, and lazy loading will fail.

* Configure lazy="false" on <class>.

2.1.2Association level lazy loading

After querying an object, obtain its associated objects. Whether to use lazy loading when querying associated objects is called association level delay.

Customer c = session.get(Customer.class,1l);
c.getLinkMans(); // Whether to use lazy loading when querying associated objects.

The level of association delay is often used with crawling strategies to optimize programs. (The level of association delay is in <set> or <many-to-one>Lazy loading on tags)

2.2Fetch strategy

Fetch strategy refers to the strategy adopted when fetching associated objects after finding an object. Fetch strategy is on the configuration of associated objects (<set> and <many-to-one>)Configure the fetch attribute.

2.2.1 lazy and fetch configured on set

fetch: Fetch strategy, controls the format of SQL statements sent.

    * select    : Default value. Send a select statement to query associated objects.

    * join  : Send an eager left outer join query to query associated objects.

    * subselect : Send a subquery to query associated objects.

lazy: Lazy loading, controls the timing of SQL statement sending.

    * true  : Default value. Use lazy loading.

    * false : Do not use lazy loading.

    * extra : and lazy.

2.2.2 in many-to-lazy and fetch configured on one

fetch: Fetch strategy, controls the format of SQL statements sent.

    * select    : Default value. Send a select statement to query associated objects.

    * join  : Send an eager left outer join query to associate objects.

lazy: Lazy loading, controls the timing of SQL sending.

    * proxy : Default value. Whether to use lazy loading depends on the lazy loading of the other class.

    * false : Do not use lazy loading.

    * no-proxy:

2.2.3 Batch Fetching

  Batch Fetching: When querying multiple customers, query all contacts under multiple customers.

  Configure batch on <set> in Customer.hbm.xml-size="n"

That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Shouting Tutorial!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like