際際滷

際際滷Share a Scribd company logo
JPA
襦 螻 磯 JPA
2019-3-20 伎
Agenda
1. JPA Basic Concept
2. Entity Manager
3. Lazy Load
4. OSIV Pattern
5. Anti-Patterns
1. JPA basic concepts
( from Pro JPA2, 2nd Edition )
1. JPA basic concepts
a. Persistence : bootstrap class used to obtain an entityManagerFactory
1. JPA basic concepts
b. EntityManagerFactory
- EntityManager襯 螳語り鍵  螳豌
- SpringFramework  企 れ EntityManagerFactoryBean  螻牛.
- EntityManagerFactory Persistence Unit 襦 .
- EntityManagerFactory 焔 貉れ 螳 覯襷 燕螻 螻旧伎 .
1. JPA basic concepts
c. Persistence Unit
- Named configuration declaring the entity classes and data store info
- SpringBoot  properties 曙伎 朱 れ 螳.
- persistence.xml  蠍一.
1. JPA basic concepts
d. EntityManager
- Jpa 覈 覿覿 蟲 企麹.
- @PersistenceContext ( ろ襷  企 覦朱 .. れ   覲旧″ 貅伎れ 螻殊 .)
1. JPA basic concepts
d. Persistence Context
- EntityManager螳 PersistenceContext襯 伎 entity  蟯襴襯 .
< Entity Lifecycle, 100覯 螳譟壱企 讌豺讌 . >
- 1谿 貂, 殊 覲伎, 覲蟆 螳讌
2. Entity Manager
a. Flush : 一危磯伎れ 覦 蟆 flushing 企手 .
- 螳 Flush : entityManager.flush()
- 碁 commit
- 貎朱Μ ろ   貉ろ語 螳 貎朱Μ 蟆郁骸 レ 覩語り   蟆曙
- FlushMode襯 れ朱 覲蟆 螳 . ( Always, Auto, Commit, Manual )
Q. FulshMode.Auto  SpringDataJpa 襯   語  蟾 ?
- Entity entityA = repositoryA.save(entityA)
- entityA.setAttributeX(Y);
- entityA = repositoryA.save(entityA);
JPA 蟲豌 覲襦 蠏  危 蟆曙磯  .
2. Entity Manager
b. Detached : entity螳 persistent 襯 覯企 
- detach : entityManager.detach(entity)
- context clear : entityManager.clear()
- session 譬襭( close )
Persistent  Entity螳 Detached 襦
  螻 Transaction    
Session Close   !!
2. Entity Manager
c. Quiz : 覈視 螻褐 覈 谿場 覲伎語.
3. Lazy Load
a. Proxy Object
- What is di鍖erence? (in Spring Data JPA )
repository.getOne(Pk id);
repository.鍖ndOne(Pk id);
repository.鍖ndById(Pk id);
https://docs.spring.io/spring-data/data-jpa/docs/current/api/
- Spring Data Jpa JavaDoc
3. Lazy Load
b. What is Lazy Load ?
4. OSIV
a. What is OSIV ( Open Session In View )
: 觀 襷  蟾讌  貉ろ碁ゼ ろ 豈襦 讌 蟆
( To avoid the LazyInitializationException )
DB Connection ? DB Flush ? Transaction ?
Detached ? Commit ?
When ?
4. OSIV
b. Using Servlet Filter
==> DB Flush, Release Connection, Detached State
覓語 : 覈誤 碁 蟆所, DB 貉るレ 覲伎 螳 讀螳
4. OSIV
c. How to use in Spring Framework
- OpenSessionInVewFilter
- OpenSessionInViewInterceptor
1. Session Open -> FlushMode.MANUAL
2. Save currentSession into ThreadLocal ( to use single session )
3. @Transactional
- Get single session from ThreadLocal & change FlushMode.AUTO
- Get Connection, begin Tx
- Execute Tx
- Commit ( Flush ) or Rollback
- Release Connection
- Change FlushMode.MANUAL
4. LazyLoad
@Transactional  覦 讌  覲蟆暑 蟷 Flush
- Get Connection ( auto commit mode )
- Execute & Release Connection
5. Session close ( change detached state )
4. OSIV
c. How to use in Spring Framework
4. OSIV
d. How to use in SpringBoot
- open-in-view : true ( default option )
( From https://terasolunaorg.github.io )
4. OSIV
e. Is it Anti-Pattern?
- Transaction 讌 螳 ( 旧 觚襴  覦 )
- 覈誤 Transaction 蟆所 (旧 觚襴  覦)
- 豺  一危一  ( until session close )
- 覃覈襴 覓語襦 誤 焔レ or OOM Exception
- 語讌 覈詩 Query  (ex: N+1 Query )
OSIV 伎 襦  る 
- 觜 伎 ろ 覈 query 覈磯
- 螳覦  JPA 覦 OSIV 伎 襦 危 螻 伎狩.
企 蠏覈螳  朱  (螳語 蟆 伎襷  )
螳覦  貉碁,  る ..
OSIV 伎 讌 覈詩蟆 螳 蟆 讌 蟾 ?
4. OSIV
f. Quiz ( open-in-view = true 手 螳 螻 )
4. OSIV
g. More details ..
http://pds19.egloos.com/pds/201106/28/18/Open_Session_In_View_Pattern.pdf
http://aeternum.egloos.com/2798098
5. Anti-Patterns
a. OneToMany 覦
Company - Branch 1:N 蟯螻 譟一語 譯殊牡 Branch 企.
讀, Branch  @ManyToOne 襷ろ 覃伎 companyId襯 譟一語錆殊朱  .
-  oneToMany 覦ル 朱  !
覲 襷ろ 企 朱  蟇磯 ,,,
Insert  讌  Update 貎朱Μ螳 ろ 蠍磯 .
( 朱 OneToMany JoinColumn 讌 蟆曙磯 蠍 讌襷 ,,, )
5. Anti-Patterns
b. Batch task
 蟯襴   ( Persistence Context 伎 )
Entity襯 螻朱蟆 譟壱  ( ex: Excel Download )
5. Anti-Patterns
c. OneToMany Outer Join
蠍磯 伎 ..
蠍磯 蟆郁骸 襦  ろ 讌襷 ,,,
HHH000104: firstResult/maxResults specified with collection fetch
Limit 譟郁唄 讌 螻 譟郁唄 襷 豌 一危磯ゼ 譟壱  覃覈襴   襷 蟆郁骸襯 豢豢 譴.
企郁唄  ろ 譟一姶 讌 襦  -_-
譬  蟆 蠏豺 覦 JPA 蟲豌願 朱 襷 譬蟆.
5. Anti-Patterns
d. N+1 Query
OSIV れ  る 
View 襷  DB Connection N覯 螳語 Query N覯 ろ
5. Anti-Patterns
e. DDD ( 螻朱 .. )
5. Anti-Patterns
f. Without QueryDsl
1. Spring Data JPA
2. @NamedQuery
3. @Query ( jpql / native )
5. Anti-Patterns
g. Long-Term Transaction
JPA  蟲 覓語 讌襷 ..
The end
- 覈視 伎 る ?
- 蟠蠍 伎 る ?
http://donny00.tistory.com
jonghee.lee@gmail.com
donny.00@kakaopaycorp.com

More Related Content

Jpa more

  • 1. JPA 襦 螻 磯 JPA 2019-3-20 伎
  • 2. Agenda 1. JPA Basic Concept 2. Entity Manager 3. Lazy Load 4. OSIV Pattern 5. Anti-Patterns
  • 3. 1. JPA basic concepts ( from Pro JPA2, 2nd Edition )
  • 4. 1. JPA basic concepts a. Persistence : bootstrap class used to obtain an entityManagerFactory
  • 5. 1. JPA basic concepts b. EntityManagerFactory - EntityManager襯 螳語り鍵 螳豌 - SpringFramework 企 れ EntityManagerFactoryBean 螻牛. - EntityManagerFactory Persistence Unit 襦 . - EntityManagerFactory 焔 貉れ 螳 覯襷 燕螻 螻旧伎 .
  • 6. 1. JPA basic concepts c. Persistence Unit - Named configuration declaring the entity classes and data store info - SpringBoot properties 曙伎 朱 れ 螳. - persistence.xml 蠍一.
  • 7. 1. JPA basic concepts d. EntityManager - Jpa 覈 覿覿 蟲 企麹. - @PersistenceContext ( ろ襷 企 覦朱 .. れ 覲旧″ 貅伎れ 螻殊 .)
  • 8. 1. JPA basic concepts d. Persistence Context - EntityManager螳 PersistenceContext襯 伎 entity 蟯襴襯 . < Entity Lifecycle, 100覯 螳譟壱企 讌豺讌 . > - 1谿 貂, 殊 覲伎, 覲蟆 螳讌
  • 9. 2. Entity Manager a. Flush : 一危磯伎れ 覦 蟆 flushing 企手 . - 螳 Flush : entityManager.flush() - 碁 commit - 貎朱Μ ろ 貉ろ語 螳 貎朱Μ 蟆郁骸 レ 覩語り 蟆曙 - FlushMode襯 れ朱 覲蟆 螳 . ( Always, Auto, Commit, Manual ) Q. FulshMode.Auto SpringDataJpa 襯 語 蟾 ? - Entity entityA = repositoryA.save(entityA) - entityA.setAttributeX(Y); - entityA = repositoryA.save(entityA); JPA 蟲豌 覲襦 蠏 危 蟆曙磯 .
  • 10. 2. Entity Manager b. Detached : entity螳 persistent 襯 覯企 - detach : entityManager.detach(entity) - context clear : entityManager.clear() - session 譬襭( close ) Persistent Entity螳 Detached 襦 螻 Transaction Session Close !!
  • 11. 2. Entity Manager c. Quiz : 覈視 螻褐 覈 谿場 覲伎語.
  • 12. 3. Lazy Load a. Proxy Object - What is di鍖erence? (in Spring Data JPA ) repository.getOne(Pk id); repository.鍖ndOne(Pk id); repository.鍖ndById(Pk id); https://docs.spring.io/spring-data/data-jpa/docs/current/api/ - Spring Data Jpa JavaDoc
  • 13. 3. Lazy Load b. What is Lazy Load ?
  • 14. 4. OSIV a. What is OSIV ( Open Session In View ) : 觀 襷 蟾讌 貉ろ碁ゼ ろ 豈襦 讌 蟆 ( To avoid the LazyInitializationException ) DB Connection ? DB Flush ? Transaction ? Detached ? Commit ? When ?
  • 15. 4. OSIV b. Using Servlet Filter ==> DB Flush, Release Connection, Detached State 覓語 : 覈誤 碁 蟆所, DB 貉るレ 覲伎 螳 讀螳
  • 16. 4. OSIV c. How to use in Spring Framework - OpenSessionInVewFilter - OpenSessionInViewInterceptor 1. Session Open -> FlushMode.MANUAL 2. Save currentSession into ThreadLocal ( to use single session ) 3. @Transactional - Get single session from ThreadLocal & change FlushMode.AUTO - Get Connection, begin Tx - Execute Tx - Commit ( Flush ) or Rollback - Release Connection - Change FlushMode.MANUAL 4. LazyLoad @Transactional 覦 讌 覲蟆暑 蟷 Flush - Get Connection ( auto commit mode ) - Execute & Release Connection 5. Session close ( change detached state )
  • 17. 4. OSIV c. How to use in Spring Framework
  • 18. 4. OSIV d. How to use in SpringBoot - open-in-view : true ( default option ) ( From https://terasolunaorg.github.io )
  • 19. 4. OSIV e. Is it Anti-Pattern? - Transaction 讌 螳 ( 旧 觚襴 覦 ) - 覈誤 Transaction 蟆所 (旧 觚襴 覦) - 豺 一危一 ( until session close ) - 覃覈襴 覓語襦 誤 焔レ or OOM Exception - 語讌 覈詩 Query (ex: N+1 Query ) OSIV 伎 襦 る - 觜 伎 ろ 覈 query 覈磯 - 螳覦 JPA 覦 OSIV 伎 襦 危 螻 伎狩. 企 蠏覈螳 朱 (螳語 蟆 伎襷 ) 螳覦 貉碁, る .. OSIV 伎 讌 覈詩蟆 螳 蟆 讌 蟾 ?
  • 20. 4. OSIV f. Quiz ( open-in-view = true 手 螳 螻 )
  • 21. 4. OSIV g. More details .. http://pds19.egloos.com/pds/201106/28/18/Open_Session_In_View_Pattern.pdf http://aeternum.egloos.com/2798098
  • 22. 5. Anti-Patterns a. OneToMany 覦 Company - Branch 1:N 蟯螻 譟一語 譯殊牡 Branch 企. 讀, Branch @ManyToOne 襷ろ 覃伎 companyId襯 譟一語錆殊朱 . - oneToMany 覦ル 朱 ! 覲 襷ろ 企 朱 蟇磯 ,,, Insert 讌 Update 貎朱Μ螳 ろ 蠍磯 . ( 朱 OneToMany JoinColumn 讌 蟆曙磯 蠍 讌襷 ,,, )
  • 23. 5. Anti-Patterns b. Batch task 蟯襴 ( Persistence Context 伎 ) Entity襯 螻朱蟆 譟壱 ( ex: Excel Download )
  • 24. 5. Anti-Patterns c. OneToMany Outer Join 蠍磯 伎 .. 蠍磯 蟆郁骸 襦 ろ 讌襷 ,,, HHH000104: firstResult/maxResults specified with collection fetch Limit 譟郁唄 讌 螻 譟郁唄 襷 豌 一危磯ゼ 譟壱 覃覈襴 襷 蟆郁骸襯 豢豢 譴. 企郁唄 ろ 譟一姶 讌 襦 -_- 譬 蟆 蠏豺 覦 JPA 蟲豌願 朱 襷 譬蟆.
  • 25. 5. Anti-Patterns d. N+1 Query OSIV れ る View 襷 DB Connection N覯 螳語 Query N覯 ろ
  • 26. 5. Anti-Patterns e. DDD ( 螻朱 .. )
  • 27. 5. Anti-Patterns f. Without QueryDsl 1. Spring Data JPA 2. @NamedQuery 3. @Query ( jpql / native )
  • 28. 5. Anti-Patterns g. Long-Term Transaction JPA 蟲 覓語 讌襷 ..
  • 29. The end - 覈視 伎 る ? - 蟠蠍 伎 る ? http://donny00.tistory.com jonghee.lee@gmail.com donny.00@kakaopaycorp.com