2

Using JPA/Hibernate 3.6/DB2.

I have got the following exception: Caused by: org.hibernate.AnnotationException: SecondaryTable JoinColumn cannot reference a non primary key

caused by:

public class XRequest {
  @ManyToOne
  @JoinTable(
    name = "RequestBatch",
    joinColumns = @JoinColumn(name = "requestBatchID", referencedColumnName="requestBatchID"),
    inverseJoinColumns = @JoinColumn(name = "requestVersionID")
  )
  private Requestversion requestversion;
}

requestBatchID is not a primary key, but an imported key from the RequestBatch table (and there, it is indeed the primary key). Why does JoinTable have to use a primary key? I mean, didn't I just define that this is a many-to-one association? Why does it have to be a primary key?

To specify: This is what the tables look like.

XRequest (
requestId int (primary)
requestBatchId int (imported key from RequestBatch)
)

RequestBatch (
requestBatchId int (primary)
requestVersionId int 
)

RequestVersion (
requestVersionId int (primary)
)

The wanted outcome is this SQL query to be built for me by Hibernate:

select xr, rv 
from XRequest xr
left outer join RequestBatch rb on rb.requestBatchId = xr.requestBatchId 
inner join RequestVersion rv on rb.requestVersionId = rv.requestVersionId
hugelgupf
  • 387
  • 5
  • 13

1 Answers1

0

If you read the JPA Documentation on @JoinTable, you will see descriptions for joinColumns and inverseJoinColumns mention:

The foreign key columns of the join table which reference the primary table of the entity...

I guess that is enough to understand the constraints.

d1e
  • 6,372
  • 2
  • 28
  • 41
  • 1
    You didn't read the question correctly... the table does indeed have a primary key. I'm just not using it for the @JoinTable, because the joining table (RequestBatch) doesn't use it. – hugelgupf Jun 13 '12 at 07:13
  • Association cannot reference non-primary key.. Make use of primary key, or what are you trying to accomplish? – d1e Jun 13 '12 at 07:17
  • Many requests belong to one requestbatch, and one requestbatch has only one requestversion (I changed these names from the original, it may sound weird). Since all the requests dont have the requestversionID in them (because it may change), I want to use the requestBatchId to join it in, using the requestbatch as a JoinTable. – hugelgupf Jun 13 '12 at 07:20
  • The requestBatchId is indeed the primary key of the RequestBatch table! But it isn't in the Request table, because that would destroy the ManyToOne association. – hugelgupf Jun 13 '12 at 07:20
  • So do you have any idea how I could do this anyway, if not using a JoinTable? I understand the constraint, however, the constraint is completely idiotic. I can build the query that Hibernate would build if the constraint wasn't there, and it works perfectly. (as edited in the first post) – hugelgupf Jun 13 '12 at 07:41
  • The JPA only allows a primary key of the owning table to be the foreign key to the joining table, but not a primary key of the joining table to be the foreign key to the owning table? Why not? It works perfectly the other way around as well. – hugelgupf Jun 13 '12 at 08:23
  • Did you happen to figure this out? – Thomas Beauvais Jun 27 '14 at 13:30
  • It is still not possible with JPA2.0, read [this post](http://stackoverflow.com/questions/11386610/jpa-joincolumn-issues-while-joining-on-non-primary-key-columns). There is more info on that. – d1e Jun 27 '14 at 17:58