JPA 和表视图.可以做到吗? [英] JPA and Table Views. Can it be done?

查看:47
本文介绍了JPA 和表视图.可以做到吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前有一个 Java EE 系统,我们在其中使用 JPA 映射到我们的数据库.这是一个相当完善的系统,大约有 20 个实体.

We currently have a Java EE system where we are mapping to our database using JPA. It is a fairly well developed system with about 20 entities.

我们现在被命令在所有事情上都使用视图.例如:如果我们有一个名为PERMISSION 的表,那么我们还需要一个名为PERMISSION_VIEW 的视图.基本上我们需要对每个表都这样做,我们的应用程序只能通过查询视图来访问数据.

We now have been ordered to use Views for everything. Eg: if we have a table called PERMISSION then we also need a view called PERMISSION_VIEW. Basically we need to do this to every table, and our applications can only access the data by querying the view.

现在我们所有的实体 bean 看起来像这样:

Now all our entity beans look like this :

@Entity
@Table(name = "PERMISSION")
@NamedQueries({
        @NamedQuery(name = "Permission.findByPK", query = "SELECT p FROM Permission p WHERE p.dpNum = :dpNumber"),
        @NamedQuery(name = "Permission.deleteAll", query = "DELETE FROM Permission") })
public class Permission implements Serializable {

}

  • 首先,如果只允许使用视图,如何更新表.物化视图可以解决这个问题吗?
  • 其次,如果我们只能使用视图,将需要多少重写?例如.对于每个条目,我们需要编写 @Table(name = "PERMISSION_VIEW") 来描述实体,但是,在进行更新时,它需要对 PERMISSION 表执行此操作.您到底是如何将其整合到实体 bean 中的?
    • Firstly, how is it possible to update tables if you are only allowed to use Views. Can Materialised Views work for this?
    • Secondly, how much rewriting is going to be needed, if we can only use Views? Eg. For each entiry we will need to write @Table(name = "PERMISSION_VIEW"), to describe the entity, BUT, when doing an update it needs to do that to the PERMISSION table. How on earth do you consolidate this in an entity bean?
    • 推荐答案

      有关 JPA 和数据库视图的更多信息,请参见 http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Views

      For more info on JPA and database views see, http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Views

      在 JPA 中,您可以使用 @Table 注释映射到与表相同的 VIEW.然后,您可以将视图中的每一列映射到对象的属性.视图通常是只读的,因此对象到视图的映射通常也是只读的.在大多数数据库中,视图也可以更新,具体取决于它们封装的查询复杂程度.即使对于复杂的查询,数据库触发器通常也可用于更新到视图中.

      In JPA you can map to a VIEW the same as a table, using the @Table annotation. You can then map each column in the view to your object's attributes. Views are normally read-only, so object's mapping to views are normally also read-only. In most databases views can also be updatable depending on how complex to query is that they encapsulate. Even for complex queries database triggers can normally be used to update into the view.

      这篇关于JPA 和表视图.可以做到吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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