Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix JPA code #21

Open
5 of 9 tasks
binkoni opened this issue Mar 1, 2022 · 0 comments
Open
5 of 9 tasks

Refactor and fix JPA code #21

binkoni opened this issue Mar 1, 2022 · 0 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@binkoni
Copy link
Member

binkoni commented Mar 1, 2022

https://www.jpa-buddy.com/blog/best-practices-and-common-pitfalls/
https://blog.sapzil.org/public/springcamp2019.pdf

https://stackoverflow.com/questions/7197181/jpa-unidirectional-many-to-one-and-cascading-delete

https://www.inflearn.com/questions/39769

https://madplay.github.io/post/avoid-n+1-problem-in-jpa-using-querydsl-fetchjoin

https://stackoverflow.com/questions/27106274/jpa-hibernate-on-delete-set-null

https://kapentaz.github.io/jpa/hibernate/@ManyToOne%EC%9D%98-N+1-%EB%AC%B8%EC%A0%9C-%EC%9B%90%EC%9D%B8-%EB%B0%8F-%ED%95%B4%EA%B2%B0/#

https://dabok407.tistory.com/73

Setup cascade properly

https://www.baeldung.com/jpa-cascade-remove-vs-orphanremoval
https://stackoverflow.com/questions/14875793/jpa-hibernate-how-to-define-a-constraint-having-on-delete-cascade
https://thorben-janssen.com/avoid-cascadetype-delete-many-assocations/
https://www.bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/

Orphan Removal

You can use in a parent entity, when you delete children from the list, then the children records are actually deleted from the database by JPA using genrated queries.
Currently there are no cases for Orphan Removal

ON DELETE CASCADE

You can use in a child entity. it affects the actual generated schema. When you delete parent entity, then child entities are automatically deleted by the database
Applied ON DELETE CASCADE

ON DELETE SET NULL

http://www.yannlaviolette.com/2013/09/jpa-hibernate-on-delete-set-null.html
Only @PreRemove approach is available
but if you don't want to use @onetomany you just have set null manually
Decided to use the manual approach

Use Transactional annotation properly

https://www.podo-dev.com/blogs/285
https://velog.io/@ddongh1122/Spring-Transactional-%ED%81%B4%EB%9E%98%EC%8A%A4-%EB%82%B4%EB%B6%80-%ED%98%B8%EC%B6%9C-%EB%AF%B8%EC%9E%91%EB%8F%99-%EC%9D%B4%EC%8A%88
https://stackoverflow.com/questions/4396284/does-spring-transactional-attribute-work-on-a-private-method
https://medium.com/javarevisited/spring-transactional-mistakes-everyone-did-31418e5a6d6b

Handle JPA errors properly

https://stackoverflow.com/questions/52456783/cannot-catch-dataintegrityviolationexception
https://www.baeldung.com/hibernate-initialize-proxy-exception
https://www.podo-dev.com/blogs/278
https://stackoverflow.com/questions/50150647/handling-exceptions-during-a-transactional-method-in-spring
Use approaches like AOP, inheritance, composition or callback methods
Decided to use AOP approach

TODOs

  • Add ON DELETE CASCADE
  • Add ON DELETE SET NULL
  • Use FetchType.LAZY for ManyToOne, OneToOne
  • Solve N+1 problem (with querydsl fetch join)
  • Use MappedSuperclass or Embedded annotation Use Entity inheritance
  • Use Transactional annotation properly (use only when needed, and use readonly transactional annotation)
  • Handle JPA errors properly
  • Use batch update/insert operations when needed
  • (not sure) Consider using spring data jpa Domain specifications (Specification) | QueryDslPredicateExecutor | QueryDslRepositorySupport
@binkoni binkoni added the enhancement New feature or request label Mar 1, 2022
@binkoni binkoni changed the title Refactor JPA code Refactor and fix JPA code Mar 4, 2022
@binkoni binkoni added the bug Something isn't working label Mar 4, 2022
@binkoni binkoni mentioned this issue Mar 4, 2022
@binkoni binkoni self-assigned this Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant