Composite Key and spring-data-jdbc

核能气质少年 提交于 2020-06-23 07:28:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/55513863/composite-key-and-spring-data-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!