Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composite Key and spring-data-jdbc

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?

like image 495
Alexander Petrov Avatar asked Apr 04 '19 10:04

Alexander Petrov


1 Answers

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.

like image 64
Jens Schauder Avatar answered Oct 12 '22 16:10

Jens Schauder