Forums Register Login

Inserting parent and child entities into a third entity using selectOneMenu

+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Mine's "Pro JPA 2".
+Pie Number of slices to send: Send
:) 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 :)
+Pie Number of slices to send: Send
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!
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1517 times.
Similar Threads
hibernat creating the tables without cascade
JPA Join Query to display a sectorname from a sectorID foreign key table
inheritance and schema creation using JPA annotations
Why is my Hibernate Search query not working?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:02:55.