简体   繁体   English

使用JPA时如何映射没有主键的数据库视图

[英]How to map a database view without a primary key when using JPA

  • I have views in a SQL database with no obvious primary key (composite or otherwise)我在没有明显主键(复合或其他)的 SQL 数据库中有视图
  • I would like to access them through JPA我想通过 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.

相关问题 没有实体主键的JPA映射视图 - JPA map view with no primary key to an entity 如何使用Spring Data JPA查询没有主键的表 - How to query a table without a primary key using spring data jpa 如何在JPA中映射复合主键,其中主键的一部分是外键 - How to map compound primary key in JPA, where part of Primary Key is a Foreign Key 不使用数据库生成主键 - Generate Primary key without using Database JPA,如何将Map中实体类型的值添加到主键? - JPA, How to add the value of entity type in Map to primary key? 如何通过JPA保留数据而无需主键 - How to persist data through JPA without needing a primary key 如何使用 UUID 字段而不是 JPA 的主键? - How to use a UUID field without being a primary key with JPA? 使用JPA创建复合主键时如何解决找不到IdClass属性的问题 - How to resolve IdClass Property not found issue when creating composite primary key using JPA 当第一个实体是具有指定主键的实体之后的后续实体时,如何使用 JPA 获取列表 - How to getList using JPA when the first entity is the following entity after the entity with specified primary key 当主键为“自动递增”时,如何使用JPA(EclipseLink)从数据库获取特定的“行”? - How to get a specific “row” from a DB using JPA (EclipseLink) when the primary key is Auto Incremented?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM