Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add a new OneToMany Child To a given Parent in JPA

 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two to entity Parent and Child and I want add a new child for a given parent: here is what I tried but the Child object is not persisted in the database.

Parent Entity


Child Entity



Servlet


Well, I just don't know how to do it, but I've made some search and this is what I tried so far
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to call persist on both the parent and the child, or set cascade persist in the OneToMany.

 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have tryed both but the children is not persisted in the database:

I have try to persist the child by adding this methode childDao.addChild(newChild);.

I have try to add the cascade=CascadeType.PERSIST to the OneToMany (the Parent) side too but it dont work.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've finally Solve the problem,

I've foregetted to add the second "@EJB" notification


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic