How to handle compound keys with Spring Data JDBC
Asked Answered
R

1

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)

Rime answered 16/11, 2018 at 21:6 Comment(1)
Does this answer your question? Composite Key and spring-data-jdbcCollazo
C
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.

Collazo answered 16/11, 2018 at 22:6 Comment(5)
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!Rime
@Jens Schauder Is there any change as of today ? Are compound keys still not supported?Globetrotter
@AlexandarPetrov unfortunately not.Collazo
@JensSchauder any plans on supporting this ? I noticed Embedded annotation is already in the milestone branch.Globetrotter
@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 jira.spring.io/browse/DATAJDBCCollazo

© 2022 - 2024 — McMap. All rights reserved.