Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Composite PK with FK problem

 
Geoffrey Mayeux
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Geoffrey Mayeux
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic