Java JPA 是否支持映射到 sql 视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:19:55  来源:igfitidea点击:

Does JPA support mapping to sql views?

javasqlormjpaview

提问by GBa

I'm currently using Eclipselink, but I know now days most JPA implementations have been pretty standardized. Is there a native way to map a JPA entity to a view? I am not looking to insert/update, but the question is really how to handle the @Id annotation. Every entity in the JPA world must have an ID field, but many of the views I have created do not conform to this. Is there native support for this in the JPA or do I need to use hacks to get it to work? I've searched a lot and found very little information about doing this.

我目前正在使用 Eclipselink,但我知道现在大多数 JPA 实现已经非常标准化。是否有将 JPA 实体映射到视图的本机方法?我不打算插入/更新,但问题实际上是如何处理 @Id 注释。JPA 世界中的每个实体都必须有一个 ID 字段,但是我创建的许多视图都不符合这一点。JPA 中是否有对此的本机支持,还是我需要使用 hacks 才能使其工作?我已经搜索了很多,但发现关于这样做的信息很少。

采纳答案by Nils Wloka

While using the @Idannotation with fields of directly supported types is not the only way to specify an entity's identity (see @IdClasswith multiple @Idannotations or @EmbeddedIdwith @Embedded), the JPA specification requires a primary key for each entity.

虽然将@Id注释与直接支持的类型的字段一起使用并不是指定实体身份的唯一方法(请参阅@IdClass使用多个@Id注释或@EmbeddedId使用@Embedded),但 JPA 规范要求每个实体都有一个主键。

That said, you don't need entities to use JPA with database views. As mapping to a view is no different from mapping to a table from an SQL perspective, you could still use native queries (createNativeQueryon EntityManager) to retrieve scalar values instead.

也就是说,您不需要实体来将 JPA 与数据库视图一起使用。由于映射到视图与从 SQL 角度映射到表没有区别,因此您仍然可以使用本机查询 ( createNativeQueryon EntityManager) 来检索标量值。

回答by BlairHippo

I've been looking into this myself, and I've found a hack that I'm not 100% certain works but that looks promising.

我自己一直在研究这个,我发现了一个黑客,我不是 100% 确定有效,但看起来很有希望。

In my case, I have a FK column in the view that can effectively function as a PK -- any given instance of that foreign object can only occur once in the view. I defined two objects off of that one field: one is designated the ID and represents the raw value of the field, and the other is designated read-only and represents the object being referred to.

在我的例子中,我在视图中有一个 FK 列,它可以有效地用作 PK——该外来对象的任何给定实例只能在视图中出现一次。我从一个字段中定义了两个对象:一个被指定为 ID 并代表该字段的原始值,另一个被指定为只读并代表被引用的对象。


@Id
@Column(name = "foreignid", unique = true, nullable = false)
public Long getForeignId() {
...

@OneToOne
@JoinColumn(name = "foreignid", insertable=false, updatable=false)
public ForeignObject getForeignObject() {
...

Like I said, I'm not 100% sure on this one (and I'll just delete this answer if it turns out not to work), but it got my code past a particular crash point.

就像我说的,我对这个不是 100% 确定(如果结果不起作用,我只会删除这个答案),但它让我的代码超过了一个特定的崩溃点。

Dunno if it applies to your specific situation, though. And there's an excellent chance that after 11 months, you no longer care. :-) What the hell, that "Necromancer" badge doesn't just earn itself....

不过,不知道它是否适用于您的特定情况。而且很有可能在 11 个月后,您不再关心。:-) 什么鬼,“死灵法师”徽章不只是自己挣来的....

回答by Everton Mendon?a

In my view I have a "unique" id, so I mapped it as the Entity id. It works very well:

在我看来,我有一个“唯一”的 id,所以我将它映射为实体 id。它工作得很好:

@Entity
@Table(name="table")
@NamedQuery(name="Table.findAll", query="SELECT n FROM Table n")
public class Table implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="column_a")
    private int columnA;