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 JPA Composite PK with FK problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA Composite PK with FK problem" Watch "JPA Composite PK with FK problem" New topic
Author

JPA Composite PK with FK problem

Geoffrey Mayeux
Greenhorn

Joined: Mar 02, 2010
Posts: 3
Hello everyone,

I am having trouble setting up an simple JPA mapping.

I have a first class defined like this :



The second one where A is a member of B



and the key made for B:



With this setup, I want to store A.id as a foreign key of B which is composed of A.id and date as primary key.
I am currently unable to do so, I have tried multiple setup with no luck.

When I persist B for the first time, it stores it correctly but put bpId = 0 in the database. And of course, the second time I persist a B instance it doesn't want because of duplicate primary key.

How can I achieve that ?
Thanks

[Edit] It seems I have posted this thread in the wrong forum, sorry about that and if a moderator can correct that i'd be happy.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
One of your issues is that you have @Transient on your ManyToOne, this will make the @ManyToOne ignored and the attribute unmapped.

For mapping a primary key through a @ManyToOne, this depends if you are using JPA 1.0 or 2.0, see,

http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Geoffrey Mayeux
Greenhorn

Joined: Mar 02, 2010
Posts: 3
I use JPA 2.0.

I have tried sample showed on the link you provided, but as I thought, it didn't worked.

It throws a PropertyAccessException:


javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.test.B.bp



Here is the full example:






And the session bean to test:



And the last Exception:
Caused by: java.lang.IllegalArgumentException: Can not set long field com.test.PhonePK.owner to com.test.Employee


It seems that he wants to map the Employee to the owner, which is good, but don't map Employee.id to PhonePK.owner but the Employee instance to PhonePK.owner.

What can I do ?
A. Raviski
Greenhorn

Joined: Mar 09, 2010
Posts: 1
Geoffrey Mayeux wrote:I use JPA 2.0.

I have tried sample showed on the link you provided, but as I thought, it didn't worked.

It throws a PropertyAccessException:
...
And the last Exception:
Caused by: java.lang.IllegalArgumentException: Can not set long field com.test.PhonePK.owner to com.test.Employee


It seems that he wants to map the Employee to the owner, which is good, but don't map Employee.id to PhonePK.owner but the Employee instance to PhonePK.owner.

What can I do ?


I got exactly the same PropertyAccessException problem using JPA 1 with hibernate 3.2.1 ga implementation (in addition to http://opensource.atlassian.com/projects/hibernate/browse/HHH-4256 for the record).
The sample looks correct according to JPA (2) specification. I'm stucked. I'll check JPA 1 specification.
Did you find any thing on your side ?
Geoffrey Mayeux
Greenhorn

Joined: Mar 02, 2010
Posts: 3
Hello,

I solved the problem.

It comes from the JPA implementation, which isn't JPA 2. Check your configuration.
If you are using Jboss AS 5.1, JPA 2 is not implemented in it.
It can be added to a project if you put directly the library in it and reference it in the classpath.

I didn't wanted to bother with that, so I switched to Jboss AS 6.0 M2 as I have no production plans atm, but I'll probably have to put the JPA2 implementation onto my project later anyway.

Hope this helps !

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA Composite PK with FK problem