Should I let JPA or the database cascade deletions?

前端 未结 3 1713
眼角桃花
眼角桃花 2021-02-05 19:23

Let\'s say we have two entities, A and B. B has a many-to-one relationship to A like follows:

@Entity
public class A {
  @OneToMany(mappedBy="a_id")
  p         


        
3条回答
  •  不思量自难忘°
    2021-02-05 19:52

    This answer raises some really strong arguments about why it should be JPA that handles the cascade, not the database.

    Here's the relevant quote:

    ...if you would make cascades on database, and not declare them in Hibernate (for performance issues) you could in some circumstances get errors. This is because Hibernate stores entities in its session cache, so it would not know about database deleting something in cascade.

    When you use second-level cache, your situation is even worse, because this cache lives longer than session and such changes on db-side will be invisible to other sessions as long old values are stored in this cache.

提交回复
热议问题