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
2 Answers
Reset to default 0If 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;
本文标签:
版权声明:本文标题:hibernate - How to use @ManyToMany when reading Entity but ignore it when saving Entity in Spring Boot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971300a2407854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论