admin管理员组

文章数量:1315069

I want to use @ManyToMany Relationship when reading Entitiy in order to get all the children to display them.

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
    name               = "ENTITY_BUTTONS",
    joinColumns        = @JoinColumn(name = "ENTITY_ID"),
    inverseJoinColumns = @JoinColumn(name = "BUTTON_ID")
)
private List<Button2> entityButtons;

But when using the same Entity from another Endpoint, which is supposed to only update Entity Columns, I want @ManyToMany Relationship to be ignored. In other words when updating Entity, and this @ManyToMany Relationship is empty List, I don't want it to delete all the Records from the Junction Table.

This is because I have separate Entitiy that is used for the Junction Table and I will be updating it through that.

So something similar to updatable = false with @ManyToOne relationship as shown below

@ManyToOne(fetch = FetchType.LAZY)    
@JoinColumn(name = "ICON_ID", insertable = false, updatable = false)    
@OnDelete(action = OnDeleteAction.CASCADE)    
private Icon icon;

I want to use @ManyToMany Relationship when reading Entitiy in order to get all the children to display them.

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
    name               = "ENTITY_BUTTONS",
    joinColumns        = @JoinColumn(name = "ENTITY_ID"),
    inverseJoinColumns = @JoinColumn(name = "BUTTON_ID")
)
private List<Button2> entityButtons;

But when using the same Entity from another Endpoint, which is supposed to only update Entity Columns, I want @ManyToMany Relationship to be ignored. In other words when updating Entity, and this @ManyToMany Relationship is empty List, I don't want it to delete all the Records from the Junction Table.

This is because I have separate Entitiy that is used for the Junction Table and I will be updating it through that.

So something similar to updatable = false with @ManyToOne relationship as shown below

@ManyToOne(fetch = FetchType.LAZY)    
@JoinColumn(name = "ICON_ID", insertable = false, updatable = false)    
@OnDelete(action = OnDeleteAction.CASCADE)    
private Icon icon;
Share Improve this question asked Jan 30 at 10:55 ivoronlineivoronline 1,1052 gold badges11 silver badges24 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If your Column Entity is the owning side of the many-to-many relation changes to the Button-List will be reflected in the DB.

You could just check for null manually and call Hibernate.initialize(yourColumn.getEntityButtons()) to manually re-attach the List before your update.

use CascadeType.PERSIST

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(
    name = "ENTITY_BUTTONS",
    joinColumns = @JoinColumn(name = "ENTITY_ID"),
    inverseJoinColumns = @JoinColumn(name = "BUTTON_ID")
)
private List<Button2> entityButtons;

本文标签: