复合键和spring-data-jdbc [英] Composite Key and spring-data-jdbc
问题描述
我有一个使用复合键的数据库.是否可以利用spring-data-jdbc?我尝试使用以下方式映射实体的里程碑版本1.1M2:
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)
}
我跟踪了SQL,但没有成功.嵌入式部分有效,但是where子句不正确.它使用的是单个参数持有人,而不是使用常规的组合键结构,其中element1 = subkey1和element2 = subkey2等等……
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...
我有两个问题.有什么方法可以使Composite ID起作用?
I have two questions. Is there any way to make the Composite ID work?
第二个问题是,假设我在findByID
顶部使用自定义@Query
,那么在这种情况下,SAVE方法是否可以工作?根本没有ID,如果我只选择一个随机列并说您将成为我的ID,该怎么办?
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?
推荐答案
否@Embedded
不适用于ID,还.
现在,所有SQL语句都为id列假定一个简单值.
而且我认为没有解决方法.
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.
这篇关于复合键和spring-data-jdbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!