• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inserting parent and child entities into a third entity using selectOneMenu  RSS feed

 
Jay Tai
Ranch Hand
Posts: 222
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The company entity contains companyName, Sector and Segment columns. The mapping is 3 entities (Company, Sector, Segment)
where Sector and Segment are used to create a company record. Sector has a OneToMany relationship with Segment and with Company.
I put the Sector and Segment values into two select menus as use these to create a Sector and Segment reference for the Company table.
I'm getting the following exception:





I'm thinking that the problem is that since the Segment entity is a child of Sector it must be entered through an instance
of Sector. Because it's being entered as a separate value I'm getting this error. The problem is Segment is defined as a Set
in the Sector entity and I can't figure out how to declare Segment as an instance using its parent entity (Sector).

If that is is not the problem I would surely appreciate some advice on where I'm going wrong. Thanks!


My code is as follows, starting with the Sector entity:



Then the Segment:


This is the company entity and the 'target' entity into which I'm trying to persist
Sector and Segment values:


Using this bean:


Finally the JSF page used to create the Company record:
 
Jay Tai
Ranch Hand
Posts: 222
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I solved this. The mapping of my tables was plain wrong. The way the company table was mapped created a physical column in the database and when there was no corresponding entity to populate that column it I got an integrity constraint violation exception. The solution was to re-write the company, sector and segment entities, using the 'mapped by' property in the @OneToMany relationship annotation. Mapped by clarifies the owning part of the relationship and prevents physical columns from being unnecessarily created by simply using the entity IDs (where my code before used foreign keys that were created as columns).

Please let me know if I need to re-post this in the JPA forum or if I should post the code of the solution here in case it helps anybody. Thanks, J
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got tons of these things, so I've lots of practice. Worse yet, they have space-padded compound keys.

The thing that saved what little sanity I still have is a Apress book by Keith and Schincariol. Doesn't deal with ALL the insanity I've encountered, but it has been a good reference for a lot of JPA arcane problems.
 
Jay Tai
Ranch Hand
Posts: 222
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case I have any sanity left after learning JSF, would you happen to know the neame of the book. I read one called 'Pro JPA' which was very well written and helpful (don't have the author's name off the top of my head). Thanks, J
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mine's "Pro JPA 2".
 
Jay Tai
Ranch Hand
Posts: 222
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:) Pardon the pun but then we're on the same page. It's written by Keith & Schincariol? One of the best books I've ever read on the subject :)
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pun accidental. I got started in JPA at version 1, but the extra features in JPA2 have been invaluable to me. And yes, they're the ones!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!