So I have two entites Columns and Notes. Many notes in one column. The relationship I have is unidirectional. Only notes know about the columns. I want if I delete a column all notes to be deleted. Is there a way I can achieve this while keeping the relationship the same? Or do I have to do it manually? (I don't want to add the relationship because the mapping will be pain in the ass)
#Spring Data JPA
41 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @craggy tree! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Well what should happen with the notes if you delete the column?
Can notes without a column exist?
Can you show your entities?
oh I misread that
I thought that's what happens and you don't want that
you can configure cascading on deletion
yes
Can you show your Note entity?
I'm asking if it's possible to do it on the child side ManyToOne side
?
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@NotNull
private String name;
@NotBlank
@NotNull
private String description;
@NotNull
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "ColumnsId")
@Cascade({org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Column column;
...
just add CascadeType.DELETE there
public class Column {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@NotNull
private String name;
//skipped stuff here
private Whiteboard whiteboard;
@NotNull
private Long position;
won't that make it so when I delete a note it deletes the column?
hmm lemme try
maybe @OnDelete(action = OnDeleteAction.CASCADE)?
maybe orphanRemoval works
ManyToOne doesn't have that
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHA
oh it seems to be OneToMany
it allows me to do that but probably will be ignored. trying it rn
still note deleted the column so I guess it does the same thing as jpa's .REMOVE
https://github.com/doctrine/orm/issues/6809 guess people don't agree that it should exist
(not jpa but a random orm discussion)
I guess I'll have to do the notes(children) deletion manually. Kinda goes against the ORM way but it will be way bigger pain in the ass reworking other shit to make that relationship possible.
I'll leave this open if anyone has any new ideas
(that's how you get into technical debt bois
)
(thank god I won't spend more than 1-2 months more on that project)
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.