12

I have a database which is using composite keys. Is it possible to utilize spring-data-jdbc? I tried the milestone version 1.1M2 where I mapped my entity in the following way:

class History {

   @ID
   @Embedded
   private CompositeHistoryID  id;
}

Then in my repository class, I added

HistoryRepository extends Repository<History,CompositeHistoryID  >{
   History  findByhId(CompositeHistoryID  id)
}

I traced the SQL, and it did not work. The embedded part worked, but the where clause was not correct. It was using a single parameter holder instead of having the regular composite key structure where element1=subkey1 and element2=subkey2 and so on...

I have two questions. Is there any way to make the Composite ID work?

The second question is, let's suppose that I use a custom @Query on top of the findByID will the SAVE method work after that if? What is there is no ID at all, what if I just pick a random column and say you will be my ID?

Alexander Petrov
  • 9,204
  • 31
  • 70
  • Related: https://stackoverflow.com/questions/53345366/how-to-handle-compound-keys-with-spring-data-jdbc/53346069#comment97738382_53346069 – Jens Schauder Apr 05 '19 at 05:50
  • 2
    Bummer that https://jira.spring.io/browse/DATAJDBC-352 was created over a year ago and such a fundamental feature has still not made it into a release. – ccampo Aug 11 '20 at 13:35
  • @ccampo upvote, hopefuly the right person will notice one day :) – Alexander Petrov Aug 11 '20 at 13:51

1 Answers1

9

No @Embedded doesn't work for Ids, yet. Right now all SQL statements assume a simple value for the id column. And I don't think there is a workaround for that.

One workaround that might work is to create a view that presents the composite key as a single field and has triggers writing the correct data into the underlying table.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348