FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL

原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql

 

In the tutorial, JavaSampleApproach will help you understand Hibernate Lazy Fetch Type and Eager Fetch Type by sample code with Spring Boot & MySql database.

I. Technologies

– Java 1.8 – Maven 3.3.9 – Spring Tool Suite – Version 3.8.1.RELEASE – Spring Boot: 1.5.1.RELEASE – MySql database

II. Hibernate Lazy Fetch & Eager Fetch Type

How to load all entities of relationships in RDBMS Database? -> Hibernate provides 2 strategies to retrieve entities(records) of all relationships: Lazy Fetch & Eager Fetch. So Hibernate Fetch Types are always associated with relationship annotations: @OneToOne, @OneToMany, @ManyToOne, @ManyToMany.


1. Eager Fetch Type

Hibernate Eager Fetch Type comes with setting: fetch = FetchType.EAGER. Details:

 

Hibernate Eager Fetch Type will load all the relationship entities at the initial time.

– Example, when calling companyRepository.findAll(), all the records of product tables will be loaded by Hibernate and stored in Set products of a Company object.

Details: – CompanyServices.java:

 

 

Logs:

 

 

From the logs, having three select statements when calling function: companyRepository.findAll() to load company entities and all relationship entities: products at initial time. Then for next processing step(showing them on console), all info of product entities are ready in memory for getting.

*** Note: should consider about memory & performance issues when working with Eager fetching strategy.

2. Lazy Fetch Type

Hibernate Lazy Fetch Type comes with setting: fetch = FetchType.LAZY.

Details:

 

Hibernate Eager Fetch Type will NOT load any entities (records) of relationships at the initial time. Need @Transaction annotation for Lazy Fetch to associate relationship loaded entities with a Hibernate session. If NOT, an exception will be thrown:

 

Details company loading service: – CompanyServices.java

 


Logs when calling the function: companyRepository.findAll():

 

Difference with Eager Fetch, at retrieving Company entities step, just having one select statement to load company entities while invoking the function: companyRepository.findAll(). When showing relationship entities(products) info, having 2 others select statements to load product records from database.

III. Practice

Step to do: – Create SpringBoot project – Create Models – Create JPA Repositories – Implement Services – Configure Datasource & Spring JPA – Implement a test Client – Run & Check results

1. Create SpringBoot project

– Using SpringToolSuite, create a SpringBoot project. Then add needed dependencies:

 

 

2. Create Models

2 Entities Company and Product that having One-to-Many relationship:

Spring Jpa One To Many Relationship

2.1 Company entity

 

 

2.2 Product entity

 

 

@Entity: Specifies that the class is an entity. This annotation is applied to the entity class. @Id: Specifies the primary key of an entity. @OneToMany: Defines a many-valued association with one-to-many multiplicity. @ManyToOne: Defines a single-valued association to another entity class that has many-to-one multiplicity @JoinColumn: Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.

3. Create JPA Repositories

Create 2 interface repositories by extends JpaRepository: – CompanyRepository.java

 

ProductRepository.java

 

 

4. Implement Services

CompanyServices.java:

 

 

ProductServices.java:

 

 

About showData() function, we must implement with @Transactional for Lazy Fetch Type. But @Transactional is optional with Eager FetchType.

5. Configure Datasource & Spring JPA

Open application.properties, configure spring.datasource & spring.jpa:

 

 

6. Implement a test Client

Use 2 repository: CompanyRepository & ProductRepository

 

 


Implement 3 functions: – clearData() is used to empty 2 tables company & productsaveData() is used to persist entities (Company & Product) to database – showData() is used to load all records (Company & Product) and show all on console.

Full SourceCode:

 

 

7. Run & Check results
7.1 Run with Eager Fetch Type

Build & Run the project with SpringBoot App mode. The sourcecode is ready with Eager Fetch Type config for both Company & Product. – Logs:

 

 

7.2 Run with Lazy Fetch Type

Change the Fetch Type to LAZY for both: Company & Product.

Company.java:

 

 

Product.java:

 

– Build & Run again the project with SpringBoot App mode. – Logs:

 

 

7.3 Mix Lazy & Eager Fetch Types

Change fetch type: Company with FetchType.LAZY BUT Product with FetchType.EAGER.

Company.java:

 

 

Product.java:

 

 

Build & Run again the project with SpringBoot App mode.

Logs:

 

 

IV. Sourcecode

SpringHibernateFetchType
By grokonez | April 27, 2017.

转载于:https://www.cnblogs.com/marcocao/p/9454642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值