#Spring Data JPA

41 messages · Page 1 of 1 (latest)

craggy tree
#

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)

lunar domeBOT
#

This post has been reserved for your question.

Hey @craggy tree! Please use /close or the Close Post button 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.

spiral viper
#

Well what should happen with the notes if you delete the column?

#

Can notes without a column exist?

#

Can you show your entities?

craggy tree
spiral viper
#

oh I misread that

#

I thought that's what happens and you don't want that

#

you can configure cascading on deletion

craggy tree
#

yes

spiral viper
#

Can you show your Note entity?

craggy tree
#

I'm asking if it's possible to do it on the child side ManyToOne side

spiral viper
#

?

craggy tree
#
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;
...
spiral viper
#

just add CascadeType.DELETE there

craggy tree
#
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;
craggy tree
spiral viper
#

I think it's the other way

#

but

craggy tree
#

hmm lemme try

lunar domeBOT
craggy tree
#

yes it's like I said

#

if I delete a note it tries to delete the column

spiral viper
#

maybe @OnDelete(action = OnDeleteAction.CASCADE)?

craggy tree
#

did nothing

#

I guess it isn't possible to do it backwards like I'm trying to do

spiral viper
#

maybe orphanRemoval works

craggy tree
#

ManyToOne doesn't have that

spiral viper
#

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHA

spiral viper
craggy tree
#

still note deleted the column so I guess it does the same thing as jpa's .REMOVE

#

(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

craggy tree
#

(that's how you get into technical debt bois kool)

#

(thank god I won't spend more than 1-2 months more on that project)

lunar domeBOT