[英]How to map a database view without a primary key when using JPA
I've read that I should be able to treat views in JPA like I treat tables (using the @Table annotation etc.).我读过我应该能够像对待表格一样对待 JPA 中的视图(使用 @Table 注释等)。 However without a primary key I have to effectively make a composite key out of EVERY COLUMN (in fact, this in what Hibernate's reverse-engineering tool seems to do by default).
但是,如果没有主键,我必须有效地从每个列中创建一个复合键(实际上,这在 Hibernate 的逆向工程工具默认情况下似乎是这样做的)。
However if I do this there are undesirable side effects.但是,如果我这样做,则会产生不良副作用。 Eg
例如
Having to write all you code pointing to the primary key's attributes rather than the views:必须编写指向主键属性而不是视图的所有代码:
myViewObject.getPrimaryKey().getFirstName() myViewObject.getPrimaryKey().getFirstName()
Not being able to use the "findBy..." methods on the spring Repository (since that attribute is part of the view's "identifier" and not actually one of it's attributes).无法在 spring 存储库上使用“findBy...”方法(因为该属性是视图“标识符”的一部分,实际上并不是它的属性之一)。
My question is: How do I map views in such a was as I can easily access their attributes using JPA?我的问题是:如何在这样的情况下映射视图,因为我可以使用 JPA 轻松访问它们的属性?
Note: I'm quite happy to be told I'm using completely the wrong approach.注意:我很高兴被告知我使用了完全错误的方法。 This seems like such a common problem there's bound to be a better solution.
这似乎是一个普遍存在的问题,肯定会有更好的解决方案。
您可以向视图的每一行添加一个UUID
列,这样您就可以将 UUID 列用作@Id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.