ManyToOne relation, parent can update/remove/add childs, but parent list is also updated when child is...
Imagine an scenario like:
CREATE TABLE PARENT (id BIGINT, name VARCHAR);
and
CREATE TABLE CHILD (id BIGINT, name VARCHAR, parent_id BIGINT FOREIGN KEY REFERENCES PARENT(id));
This, translated in JPA entities, would be something like:
Parent.java:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
private List<Child> child;
Child.java:
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) //Child should not modify parent (except for its List<> of childs)
@JoinColumn(name = "parentId", updatable = false, insertable = false)
private Parent parent;
I don't know how to achieve the following setting:
- When parentRepository.saveOrMerge(parent) is executed, both parent and its child on the set should be persisted. If parent's child Set has been updated from db state, changes should applied to child table as well. (This is the orphanRemoval flag, I guess). If the state of a particular child has changed, this should be persisted as well (this is the CascadeType.ALL on the parent-side, I guess).
When parentRepository.remove(parent) is executed, both parent and its child should be removed. Like an ON DELETE CASCADE, anything that references parent should be deleted even if the managed entity had a Child Set cleared. I dont know if this is possible through JPA.
When childRepository.saveOrMerge(child) is executed, child should be persisted, and if the parent object is not persisted, it should be persisted as well. In case the parent is in db and the child has a modified instance, changes should be discarded. (I guess this is the updatable=false and insertable=true) flags
When childRepository.remove(child) is executed, child should be removed from db, AND from any parent's set in memory. If this was the only child the parent had, parent should not be deleted (I guess this is the omission of CascadeType.REMOVE on the child-side relation)
Currently with the previous code, bullet point #4 is not achievable. Is there anyway I can indicate parent's child set should be automatically updated if I childRepository.remove(child) only by means of JPA annotations in the entity ? I would like to avoid having to use parentRepository when I want to delete a child. I find unnatural the only way to achieve all of this is by removing my child from the parent set and saving parent. What's the point of a ChildRepository then, if updating the Set in the parent and saving the parent is enough for everything?
java hibernate jpa spring-data
|
show 5 more comments
Imagine an scenario like:
CREATE TABLE PARENT (id BIGINT, name VARCHAR);
and
CREATE TABLE CHILD (id BIGINT, name VARCHAR, parent_id BIGINT FOREIGN KEY REFERENCES PARENT(id));
This, translated in JPA entities, would be something like:
Parent.java:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
private List<Child> child;
Child.java:
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) //Child should not modify parent (except for its List<> of childs)
@JoinColumn(name = "parentId", updatable = false, insertable = false)
private Parent parent;
I don't know how to achieve the following setting:
- When parentRepository.saveOrMerge(parent) is executed, both parent and its child on the set should be persisted. If parent's child Set has been updated from db state, changes should applied to child table as well. (This is the orphanRemoval flag, I guess). If the state of a particular child has changed, this should be persisted as well (this is the CascadeType.ALL on the parent-side, I guess).
When parentRepository.remove(parent) is executed, both parent and its child should be removed. Like an ON DELETE CASCADE, anything that references parent should be deleted even if the managed entity had a Child Set cleared. I dont know if this is possible through JPA.
When childRepository.saveOrMerge(child) is executed, child should be persisted, and if the parent object is not persisted, it should be persisted as well. In case the parent is in db and the child has a modified instance, changes should be discarded. (I guess this is the updatable=false and insertable=true) flags
When childRepository.remove(child) is executed, child should be removed from db, AND from any parent's set in memory. If this was the only child the parent had, parent should not be deleted (I guess this is the omission of CascadeType.REMOVE on the child-side relation)
Currently with the previous code, bullet point #4 is not achievable. Is there anyway I can indicate parent's child set should be automatically updated if I childRepository.remove(child) only by means of JPA annotations in the entity ? I would like to avoid having to use parentRepository when I want to delete a child. I find unnatural the only way to achieve all of this is by removing my child from the parent set and saving parent. What's the point of a ChildRepository then, if updating the Set in the parent and saving the parent is enough for everything?
java hibernate jpa spring-data
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
1
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17
|
show 5 more comments
Imagine an scenario like:
CREATE TABLE PARENT (id BIGINT, name VARCHAR);
and
CREATE TABLE CHILD (id BIGINT, name VARCHAR, parent_id BIGINT FOREIGN KEY REFERENCES PARENT(id));
This, translated in JPA entities, would be something like:
Parent.java:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
private List<Child> child;
Child.java:
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) //Child should not modify parent (except for its List<> of childs)
@JoinColumn(name = "parentId", updatable = false, insertable = false)
private Parent parent;
I don't know how to achieve the following setting:
- When parentRepository.saveOrMerge(parent) is executed, both parent and its child on the set should be persisted. If parent's child Set has been updated from db state, changes should applied to child table as well. (This is the orphanRemoval flag, I guess). If the state of a particular child has changed, this should be persisted as well (this is the CascadeType.ALL on the parent-side, I guess).
When parentRepository.remove(parent) is executed, both parent and its child should be removed. Like an ON DELETE CASCADE, anything that references parent should be deleted even if the managed entity had a Child Set cleared. I dont know if this is possible through JPA.
When childRepository.saveOrMerge(child) is executed, child should be persisted, and if the parent object is not persisted, it should be persisted as well. In case the parent is in db and the child has a modified instance, changes should be discarded. (I guess this is the updatable=false and insertable=true) flags
When childRepository.remove(child) is executed, child should be removed from db, AND from any parent's set in memory. If this was the only child the parent had, parent should not be deleted (I guess this is the omission of CascadeType.REMOVE on the child-side relation)
Currently with the previous code, bullet point #4 is not achievable. Is there anyway I can indicate parent's child set should be automatically updated if I childRepository.remove(child) only by means of JPA annotations in the entity ? I would like to avoid having to use parentRepository when I want to delete a child. I find unnatural the only way to achieve all of this is by removing my child from the parent set and saving parent. What's the point of a ChildRepository then, if updating the Set in the parent and saving the parent is enough for everything?
java hibernate jpa spring-data
Imagine an scenario like:
CREATE TABLE PARENT (id BIGINT, name VARCHAR);
and
CREATE TABLE CHILD (id BIGINT, name VARCHAR, parent_id BIGINT FOREIGN KEY REFERENCES PARENT(id));
This, translated in JPA entities, would be something like:
Parent.java:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
private List<Child> child;
Child.java:
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) //Child should not modify parent (except for its List<> of childs)
@JoinColumn(name = "parentId", updatable = false, insertable = false)
private Parent parent;
I don't know how to achieve the following setting:
- When parentRepository.saveOrMerge(parent) is executed, both parent and its child on the set should be persisted. If parent's child Set has been updated from db state, changes should applied to child table as well. (This is the orphanRemoval flag, I guess). If the state of a particular child has changed, this should be persisted as well (this is the CascadeType.ALL on the parent-side, I guess).
When parentRepository.remove(parent) is executed, both parent and its child should be removed. Like an ON DELETE CASCADE, anything that references parent should be deleted even if the managed entity had a Child Set cleared. I dont know if this is possible through JPA.
When childRepository.saveOrMerge(child) is executed, child should be persisted, and if the parent object is not persisted, it should be persisted as well. In case the parent is in db and the child has a modified instance, changes should be discarded. (I guess this is the updatable=false and insertable=true) flags
When childRepository.remove(child) is executed, child should be removed from db, AND from any parent's set in memory. If this was the only child the parent had, parent should not be deleted (I guess this is the omission of CascadeType.REMOVE on the child-side relation)
Currently with the previous code, bullet point #4 is not achievable. Is there anyway I can indicate parent's child set should be automatically updated if I childRepository.remove(child) only by means of JPA annotations in the entity ? I would like to avoid having to use parentRepository when I want to delete a child. I find unnatural the only way to achieve all of this is by removing my child from the parent set and saving parent. What's the point of a ChildRepository then, if updating the Set in the parent and saving the parent is enough for everything?
java hibernate jpa spring-data
java hibernate jpa spring-data
edited Nov 21 '18 at 16:46
Whimusical
asked Nov 21 '18 at 14:14
WhimusicalWhimusical
2,58533776
2,58533776
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
1
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17
|
show 5 more comments
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
1
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
1
1
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17
|
show 5 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414008%2fmanytoone-relation-parent-can-update-remove-add-childs-but-parent-list-is-also%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414008%2fmanytoone-relation-parent-can-update-remove-add-childs-but-parent-list-is-also%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you remove orphanRemoval=true from your child class or make it false?
– GauravRai1512
Nov 21 '18 at 14:20
its a bad idea to use any of the parameters in @ManyToOne side.
– Alien
Nov 21 '18 at 14:26
@GauravRai1512 Why? Even if I plan to enable the removal of childs directly in the entity manager, it makes sense that if I save the parent with a modified list of childs it also updates the child table
– Whimusical
Nov 21 '18 at 14:30
@Alien I was just testing, my only purpose for the non-REMOVE cascades is trying to make parent List<Child> autoupdated in case I execute a repository.remove(child)
– Whimusical
Nov 21 '18 at 14:31
1
Cascading options have zero effect on in-memory data.You need to maintain in-memory data yourself. While you may find it unnatural to work with parent repo that is a widely accepted design pattern: stackoverflow.com/questions/1958621/whats-an-aggregate-root
– Alan Hay
Nov 21 '18 at 17:17