Search code examples
javahibernatespring-data-jpaspring-datahibernate-mapping

Do CascadeType.ALL and "insertable = false, updatable = false" exclude each other?


If I have the configuration like this, can it work?

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "FOREIGN_ID", nullable = false, insertable = false, updatable = false)
ForeignClass foreignClass;

I think not, because the behaviour of the cascade types is in conflict with the insertable and updatable parameters.

What do you think?


Solution

  • They mean different things and do not conflict with each other.

    So given the following mapping :

    @Entity
    @Table(name="player")
    public class Player {
    
    
       @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
       @JoinColumn(name = "team_id", nullable = false, insertable = false, updatable = false)
       Team team;
    
    }
    
    • cascade means that when using EntityManager to persist() or merge() on a player , JPA will automatically call persist() / merge() on this player 's team too.

    • insertable and updatable is about if allow to assign a new team or update the team for a player . In term of the DB table , it is about if the value of Player table 's team_id column is allowed to be inserted or updated.

    So one is about inserting/updating records in the Team table while another is about inserting/updating the value of the Player table 's team_id column , which are totally different things.