• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to persist to 2 tables in same database (from one primefaces form)  RSS feed

 
Randy Maddocks
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, please forgive me for this very newbie type question. First, I will do my best to explain what I am trying to accomplish: I have a primefaces form containing several different kinds of fields (i.e. inputs, dropdowns, selectonemenu, etc. . .). All but 2 of the fields on the form persist to a table ("parent") in a database. The remaining 2 fields persist to a different table ("child") in the same database. The 2 tables have a relationship with one another via a primary key. I am using Netbeans 8, which automatically generated the Entity, ManagedBean and EJB classes through a CRUD Wizard. Despite having read several articles and reviewing multiple code examples, I am having trouble fully understanding how the @OneToMany / @ManyToOne process works. More specifically, how it relates to what I am trying to accomplish. I want to be able to persist the appropriate fields to the appropriate table. I have no problem persisting to the "parent" table, but cannot get the data from the 2 fields to persist to the "child" table.

I should note that I believe I understand the basis around persisting to 2 different tables that have a relationship with one another, roughly as follows:

1. Create parent class
2. Persist it
3. Create child class
4. Associate child with it's parent
5. Persist child (the parent collections is updated)

But my problem is how to take the data entered on those 2 fields on the form and get them persisted to the appropriate columns in the "child" database, based on the above high level steps.

I have not attached any code as yet, as I figured I'd start with what exactly I am having a problem with understanding. As noted, please forgive me for what is probably an easy problem to resolve. Thank you!!
 
Eric Cornely
Greenhorn
Posts: 9
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have time to explain a lot but you should have a look to @Embedded in JPA specification I think it could help you.

EDIT: Sorry this was the opposite having multiples objects mapped to one table
 
Randy Maddocks
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric, thank you for your reply. I will take a closer look at that annotation (I had read about it, but didn't pay much attention to it). Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!