Skip to content
Advertisement

JPA With composite key non standard

I’m trying to do a JPA mapping for an existing database. I can’t really change the existing structure.

I managed to make it works, but Intellij is telling me that some column doesn’t exist even if it works. So I don’t know if there’s a better way to do this or if it’s Intellij that doesn’t support all the use cases.

I simplified my mapping and table for the question. The 2 tables with primary composite keys are:

JavaScript

And my mapping is:

JavaScript

So like I said it works but I have an error in Intellij saying that the @JoinColumn(name ="someIdA") @JoinColumn(name = "someSeqA") don’t exist and is expecting something like @JoinColumn(name ="some_id_a") @JoinColumn(name = "some_seq_a").

Using it the way Intellij is telling me, JPA has en error that says: Table [table_b] contains physical column name [some_id_a] referred to by multiple logical column names: [some_id_a], [someIdA].

My mapping is ok despite Intellij but is there’s a better alternative ?

Thanks

Advertisement

Answer

You can use a “derived identity” and map your classes like this:

JavaScript

Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement