JPA 是否支持映射到 sql 视图? [英] Does JPA support mapping to sql views?

查看:22
本文介绍了JPA 是否支持映射到 sql 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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

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 (createNativeQuery on EntityManager) to retrieve scalar values instead.

这篇关于JPA 是否支持映射到 sql 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆