Reputation: 136
I have a one to many relation on post class, and on the relation table I have one to one relation with user. Everything works find, but i want to be able to remove the relation, keeping the user entity, is that possible?
At this moment with the annotation orphanRemoval = true
when I remove from post Detail list an element, this its removed from post_details table but the user is removed too.
@Entity
@Table(name = "ta_post")
public class Post{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Date fcDate;
@OneToMany(mappedBy="post", cascade=CascadeType.ALL, orphanRemoval = true)
private List<PostDetails>;
}
@Entity
@Table(name = "ta_user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int mail;
}
@Entity
@Table(name = "ta_post_details")
public class PostDetails{
private int id;
@ManyToOne
@JoinColumn(name="post_id")
private Post post;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="user_id")
private User user;
private String postComments;
}
Upvotes: 1
Views: 768
Reputation: 387
You must remove the CascadeType.ALL from the PostDetails. If you want to be able to change the User through the PostDetails, you can set the CascadeType to PERSIST or MERGE. If you want to create a PostDetail along with an User, you need to include the CascadeType CREATE.
I'd guess you are creating the user somewhere else and you just associate one with a Post, so removing the CascadeType.ALL should be enough to not delete your User from the database.
Upvotes: 1