File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes OneToOne mapping with 2 composite keys using JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne mapping with 2 composite keys using JPA" Watch "OneToOne mapping with 2 composite keys using JPA" New topic
Author

OneToOne mapping with 2 composite keys using JPA

Donnacha Lehane
Greenhorn

Joined: Aug 06, 2009
Posts: 3
TableA has a 3 column composite primary key, TableB has a 2 column composite key primary key (these 2 columns are in TableA) and TableC
has a single column primary key but has a composite foreign key from TableB.

This is an inherited table structure which I cannot change.

TableA 1:1 TableB
TableB M:1 TableA
TableB 1:M TableC
TableC M:1 TableB

UNB_NETWORK_CLINIC_PROVIDER - TABLE A
NETWORK_NO (PK)
CLINIC_NO (PK)
PROVIDER_NO (PK)
DELTA_BEGIN_DATE
DELTA_END_DATE

UNB_CLINIC_PROVIDER - TABLE B
CLINIC_NO (PK)
PROVIDER_NO (PK)
DELTA_BEGIN_DATE
DELTA_END_DATE

UNB_FOCUS_REVIEW - TABLE C
FOCUS_REVIEW_NO (PK)
PROVIDER_NO(FK)
CLINIC_NO(FK)
PROC_NO_FROM
PROC_NO_TO

I have used OneToMany JPA annotation to model the mapping between TableB and TableC using the composite key in the mappedBy attribute.
Please see code below in namely UnbClinicProv.java and UnbFocusReview.java. This worked when the composite key of TableB was a foreign key in TableC.

I tried (the same thing I thought) for the OneToOne mapping in UnbNetworkClinicProvider.java but this gives the error below.

Is it possible to model the OneToOne mapping between TableA and TableB using JPA annotations when the composite key of TableB is not a
full foriegn key in TableA?
If yes how?

Please see below for the tables, code snippets and error I get;

Any help/comments appreciated.

Error:


UnbClinicProv.java


UnbFocusReview.java


UnbNetworkClinicProvider.java


NetworkClinicProviderCompoundKey.java


ClinicProvCompoundKey.java
Donnacha Lehane
Greenhorn

Joined: Aug 06, 2009
Posts: 3
If I try and change my mapping to link through the foreign key columns rather than using the composite key I also get an error.
Again I'm not sure if this is possible and not sure which option I should be investigating further.

If anyone has any ideas whether I am on the right road on not please let me know.

Error:


UnbNetworkClinicProvider.java


UnbClinicProv.java
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

So, the exceptions are pretty good in their definition. Telling you a property is not mapped means just that - you have an instance variable in a class that has no mapping.

Telling you the db doesn't have the same number of columns as the compound primary key is pretty good too.

How were the mappings done? Did you reverse engineer or do it yourself? Sometimes just doing one class at a time isn't a bad idea either.

-Cameron McKenzie
Donnacha Lehane
Greenhorn

Joined: Aug 06, 2009
Posts: 3
Thanks for the reply.

I did the mappings myself and therin lies my problem. I got the relationships between the tables incorrect. The mapping I was having trouble with was between TableA and TableB. This is in fact a M:1 relationship and when I changed it to such it worked. Please see my working code below.

BTW, 'Hibernate made easy' is a great help, thanks.

UnbNetworkClinicProvider.java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OneToOne mapping with 2 composite keys using JPA