Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
Pixel-Killer Avatar asked Dec 07 '25 05:12

Pixel-Killer


1 Answers

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.

like image 51
Ken Chan Avatar answered Dec 08 '25 17:12

Ken Chan