Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EntityManager persist problem in oneToMany relation

 
Juan Porras Antonio
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys, good afternoon from Spain.

I need an example to do OneToMany persistence with EntityManager, my exercise doesn't work.

I have a class, Person, with ID, name and Set of Phones.

In the other side, I have a class called Phone, which contains ID, phoneNumber and FK_phone.

First question:
Do I need create ForeignKey in MySQL database?.

Second Question:
Does EntityManager persist with OneToMany?. My example saves Person, but not Phones when I do em.persist(Person).
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on the cascade option you set to the Collection of Phones inside the Person object.

Show us your Person mapping.

Mark
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!