Hibernate cascade save parent and children - parent key not found
up vote
2
down vote
favorite
I am using Spring, Hibernate and oracle for a project. The database schema was created manually by running sql script. Everything works fine until I encountered a problem with hibernate one-to-many bidirectional cascade save.
In the parent class (Product.java)
@OneToMany(mappedBy="product",
fetch=FetchType.EAGER,
cascade={CascadeType.ALL})
public Set<Picture> getPictures() {
return pictures;
}
@Transient
public void addPicture(Picture picture) {
picture.setProduct(this);
pictures.add(picture);
}
In the child class (Picture.java)
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
"lse09lse06id" is the foreign key column in the child entity table.
In my Controller class:
Product product = new Product();
.... (set properties of product)
Picture newPicture = new Picture();
.... (set properties of newPicture)
product.addPicture(newPicture);
productService.addProduct(product);
In my ProductService class:
@Override
@Transactional
public void addProduct(Product product) {
productDAO.addProduct(product);
}
In my ProductDAO class:
@Override
public void addProduct(Product product) {
product.setDateCreated(new Date());
sessionFactory.getCurrentSession().save(product);
}
Exception thrown when the controller code is executed:
org.springframework.web.util.NestedServletException:
Request processing failed; nested
exception is
org.springframework.dao.DataIntegrityViolationException:
Could not execute JDBC batch update;
SQL [insert into lse09pictures
(lse09content, lse09date_created,
lse09date_deleted, lse09date_updated,
lse09is_deleted, lse09lse06id,
lse09id) values (?, ?, ?, ?, ?, ?,
?)]; constraint
[CSSE3005GG.LSE09PICTURES_FK]; nested
exception is
org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update
One of the nested exception thrown:
java.sql.BatchUpdateException:
ORA-02291: integrity constraint
(CSSE3005GG.LSE09PICTURES_FK) violated
- parent key not found
Any help would be much appreciated. This problem is really nasty. Thank you!
hibernate
add a comment |
up vote
2
down vote
favorite
I am using Spring, Hibernate and oracle for a project. The database schema was created manually by running sql script. Everything works fine until I encountered a problem with hibernate one-to-many bidirectional cascade save.
In the parent class (Product.java)
@OneToMany(mappedBy="product",
fetch=FetchType.EAGER,
cascade={CascadeType.ALL})
public Set<Picture> getPictures() {
return pictures;
}
@Transient
public void addPicture(Picture picture) {
picture.setProduct(this);
pictures.add(picture);
}
In the child class (Picture.java)
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
"lse09lse06id" is the foreign key column in the child entity table.
In my Controller class:
Product product = new Product();
.... (set properties of product)
Picture newPicture = new Picture();
.... (set properties of newPicture)
product.addPicture(newPicture);
productService.addProduct(product);
In my ProductService class:
@Override
@Transactional
public void addProduct(Product product) {
productDAO.addProduct(product);
}
In my ProductDAO class:
@Override
public void addProduct(Product product) {
product.setDateCreated(new Date());
sessionFactory.getCurrentSession().save(product);
}
Exception thrown when the controller code is executed:
org.springframework.web.util.NestedServletException:
Request processing failed; nested
exception is
org.springframework.dao.DataIntegrityViolationException:
Could not execute JDBC batch update;
SQL [insert into lse09pictures
(lse09content, lse09date_created,
lse09date_deleted, lse09date_updated,
lse09is_deleted, lse09lse06id,
lse09id) values (?, ?, ?, ?, ?, ?,
?)]; constraint
[CSSE3005GG.LSE09PICTURES_FK]; nested
exception is
org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update
One of the nested exception thrown:
java.sql.BatchUpdateException:
ORA-02291: integrity constraint
(CSSE3005GG.LSE09PICTURES_FK) violated
- parent key not found
Any help would be much appreciated. This problem is really nasty. Thank you!
hibernate
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am using Spring, Hibernate and oracle for a project. The database schema was created manually by running sql script. Everything works fine until I encountered a problem with hibernate one-to-many bidirectional cascade save.
In the parent class (Product.java)
@OneToMany(mappedBy="product",
fetch=FetchType.EAGER,
cascade={CascadeType.ALL})
public Set<Picture> getPictures() {
return pictures;
}
@Transient
public void addPicture(Picture picture) {
picture.setProduct(this);
pictures.add(picture);
}
In the child class (Picture.java)
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
"lse09lse06id" is the foreign key column in the child entity table.
In my Controller class:
Product product = new Product();
.... (set properties of product)
Picture newPicture = new Picture();
.... (set properties of newPicture)
product.addPicture(newPicture);
productService.addProduct(product);
In my ProductService class:
@Override
@Transactional
public void addProduct(Product product) {
productDAO.addProduct(product);
}
In my ProductDAO class:
@Override
public void addProduct(Product product) {
product.setDateCreated(new Date());
sessionFactory.getCurrentSession().save(product);
}
Exception thrown when the controller code is executed:
org.springframework.web.util.NestedServletException:
Request processing failed; nested
exception is
org.springframework.dao.DataIntegrityViolationException:
Could not execute JDBC batch update;
SQL [insert into lse09pictures
(lse09content, lse09date_created,
lse09date_deleted, lse09date_updated,
lse09is_deleted, lse09lse06id,
lse09id) values (?, ?, ?, ?, ?, ?,
?)]; constraint
[CSSE3005GG.LSE09PICTURES_FK]; nested
exception is
org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update
One of the nested exception thrown:
java.sql.BatchUpdateException:
ORA-02291: integrity constraint
(CSSE3005GG.LSE09PICTURES_FK) violated
- parent key not found
Any help would be much appreciated. This problem is really nasty. Thank you!
hibernate
I am using Spring, Hibernate and oracle for a project. The database schema was created manually by running sql script. Everything works fine until I encountered a problem with hibernate one-to-many bidirectional cascade save.
In the parent class (Product.java)
@OneToMany(mappedBy="product",
fetch=FetchType.EAGER,
cascade={CascadeType.ALL})
public Set<Picture> getPictures() {
return pictures;
}
@Transient
public void addPicture(Picture picture) {
picture.setProduct(this);
pictures.add(picture);
}
In the child class (Picture.java)
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
"lse09lse06id" is the foreign key column in the child entity table.
In my Controller class:
Product product = new Product();
.... (set properties of product)
Picture newPicture = new Picture();
.... (set properties of newPicture)
product.addPicture(newPicture);
productService.addProduct(product);
In my ProductService class:
@Override
@Transactional
public void addProduct(Product product) {
productDAO.addProduct(product);
}
In my ProductDAO class:
@Override
public void addProduct(Product product) {
product.setDateCreated(new Date());
sessionFactory.getCurrentSession().save(product);
}
Exception thrown when the controller code is executed:
org.springframework.web.util.NestedServletException:
Request processing failed; nested
exception is
org.springframework.dao.DataIntegrityViolationException:
Could not execute JDBC batch update;
SQL [insert into lse09pictures
(lse09content, lse09date_created,
lse09date_deleted, lse09date_updated,
lse09is_deleted, lse09lse06id,
lse09id) values (?, ?, ?, ?, ?, ?,
?)]; constraint
[CSSE3005GG.LSE09PICTURES_FK]; nested
exception is
org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update
One of the nested exception thrown:
java.sql.BatchUpdateException:
ORA-02291: integrity constraint
(CSSE3005GG.LSE09PICTURES_FK) violated
- parent key not found
Any help would be much appreciated. This problem is really nasty. Thank you!
hibernate
hibernate
asked Feb 21 '11 at 14:09
shengloong
4058
4058
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Remove cascade
from the @ManyToOne(cascade={CascadeType.ALL})
. Hibernate gets confused by circular references.
Alternatively, save the Product
instance (by explicitly calling session.save() on it) before the Picture
instance.
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modifiedProductDAO
in the following way.@Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
add a comment |
up vote
0
down vote
Using the Hibernate Cascade classes
- org.hibernate.annotations.Cascade
- org.hibernate.annotations.CascadeType
Could you try to rewrite it like this:
@OneToMany(fetch=FetchType.EAGER,
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Picture> getPictures() {
return pictures;
}
And
@ManyToOne
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
After that, call to Hibernate.saveOrUpdate should do what you want.
Cyberax is right, circular references can be confusing for Hibernate. Plus, Cascading removal should be used with care - that is, why I only have used Save,Persist, and Merge.
add a comment |
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',
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%2f5066960%2fhibernate-cascade-save-parent-and-children-parent-key-not-found%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Remove cascade
from the @ManyToOne(cascade={CascadeType.ALL})
. Hibernate gets confused by circular references.
Alternatively, save the Product
instance (by explicitly calling session.save() on it) before the Picture
instance.
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modifiedProductDAO
in the following way.@Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
add a comment |
up vote
0
down vote
Remove cascade
from the @ManyToOne(cascade={CascadeType.ALL})
. Hibernate gets confused by circular references.
Alternatively, save the Product
instance (by explicitly calling session.save() on it) before the Picture
instance.
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modifiedProductDAO
in the following way.@Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
add a comment |
up vote
0
down vote
up vote
0
down vote
Remove cascade
from the @ManyToOne(cascade={CascadeType.ALL})
. Hibernate gets confused by circular references.
Alternatively, save the Product
instance (by explicitly calling session.save() on it) before the Picture
instance.
Remove cascade
from the @ManyToOne(cascade={CascadeType.ALL})
. Hibernate gets confused by circular references.
Alternatively, save the Product
instance (by explicitly calling session.save() on it) before the Picture
instance.
answered Feb 21 '11 at 21:57
Cyberax
1,3971317
1,3971317
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modifiedProductDAO
in the following way.@Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
add a comment |
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modifiedProductDAO
in the following way.@Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modified
ProductDAO
in the following way. @Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
Thanks for the reply. I have tried both ways but I still get the same exception when I run the code. As for the alternative you suggested, I modified
ProductDAO
in the following way. @Override public void addProduct(Product product) { product.setDateCreated(new Date()); Session session = sessionFactory.getCurrentSession(); session.save(product); for (Picture picture: product.getPictures()) { session.save(picture); } }
– shengloong
Feb 23 '11 at 12:03
add a comment |
up vote
0
down vote
Using the Hibernate Cascade classes
- org.hibernate.annotations.Cascade
- org.hibernate.annotations.CascadeType
Could you try to rewrite it like this:
@OneToMany(fetch=FetchType.EAGER,
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Picture> getPictures() {
return pictures;
}
And
@ManyToOne
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
After that, call to Hibernate.saveOrUpdate should do what you want.
Cyberax is right, circular references can be confusing for Hibernate. Plus, Cascading removal should be used with care - that is, why I only have used Save,Persist, and Merge.
add a comment |
up vote
0
down vote
Using the Hibernate Cascade classes
- org.hibernate.annotations.Cascade
- org.hibernate.annotations.CascadeType
Could you try to rewrite it like this:
@OneToMany(fetch=FetchType.EAGER,
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Picture> getPictures() {
return pictures;
}
And
@ManyToOne
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
After that, call to Hibernate.saveOrUpdate should do what you want.
Cyberax is right, circular references can be confusing for Hibernate. Plus, Cascading removal should be used with care - that is, why I only have used Save,Persist, and Merge.
add a comment |
up vote
0
down vote
up vote
0
down vote
Using the Hibernate Cascade classes
- org.hibernate.annotations.Cascade
- org.hibernate.annotations.CascadeType
Could you try to rewrite it like this:
@OneToMany(fetch=FetchType.EAGER,
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Picture> getPictures() {
return pictures;
}
And
@ManyToOne
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
After that, call to Hibernate.saveOrUpdate should do what you want.
Cyberax is right, circular references can be confusing for Hibernate. Plus, Cascading removal should be used with care - that is, why I only have used Save,Persist, and Merge.
Using the Hibernate Cascade classes
- org.hibernate.annotations.Cascade
- org.hibernate.annotations.CascadeType
Could you try to rewrite it like this:
@OneToMany(fetch=FetchType.EAGER,
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Picture> getPictures() {
return pictures;
}
And
@ManyToOne
@Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
CascadeType.MERGE })
@JoinColumn(name="lse09lse06id")
public Product getProduct() {
return product;
}
After that, call to Hibernate.saveOrUpdate should do what you want.
Cyberax is right, circular references can be confusing for Hibernate. Plus, Cascading removal should be used with care - that is, why I only have used Save,Persist, and Merge.
answered Oct 20 at 8:32
Tobi
2048
2048
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f5066960%2fhibernate-cascade-save-parent-and-children-parent-key-not-found%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