7

I'm trying to use Spring Data JDBC to access a table that has a compound key, and although I can find lots of info on how Spring Data JPA handles this, the same doesn't seem to apply to Spring Data JDBC.

Can anyone give a quick example of how you need to declare an Entity/Repository with Spring Data JDBC to talk to a table with a compound primary key ? (Eg. Two Strings)

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Ozzy
  • 71
  • 1
  • 2
  • Does this answer your question? [Composite Key and spring-data-jdbc](https://stackoverflow.com/questions/55513863/composite-key-and-spring-data-jdbc) – Jens Schauder Jun 15 '23 at 07:59

1 Answers1

2

Using the CrudRepository for compound keys is currently (Version 1.0.2) not possible.

Of course, you can still use methods annotated with @Query.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • 1
    I tried, but I have to declare at least something with `@Id`, or it refuses to read the data, and if I do that, then updates are issued with an incorrect where clause. I've ended up having to reimplement 'save' in my own class, using separate insert & update methods on the repository that have to accept each column as a named parameter because (I think?) spring-data-jdbc doesn't support spel in `@query` currently, so I can't just pass my dao & access the fields from the `@query`. If you have a better suggestion for a workaround for how perform 'save', I'd be very interested! – Ozzy Nov 16 '18 at 22:20
  • @Jens Schauder Is there any change as of today ? Are compound keys still not supported? – Alexander Petrov Apr 04 '19 at 09:37
  • @AlexandarPetrov unfortunately not. – Jens Schauder Apr 04 '19 at 11:13
  • @JensSchauder any plans on supporting this ? I noticed Embedded annotation is already in the milestone branch. – Alexander Petrov Apr 04 '19 at 11:48
  • 2
    @AlexandarPetrov Absolutely! Actually, I'm building/fixing/improving infrastructure right now that will help to make this possible. That said: I currently don't think it will make it into the 1.1 release. Could you create a ticket for it at https://jira.spring.io/browse/DATAJDBC – Jens Schauder Apr 04 '19 at 13:13