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

查看:662
本文介绍了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.

我们现在被命令对所有内容都使用Views.例如:如果我们有一个名为 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 {

}

  • 首先,如果只允许使用视图,那么如何更新表.物化视图可以为此工作吗?
  • 第二,如果我们只能使用Views,那么将需要重写多少笔?例如.对于每个条目,我们将需要编写 @Table(name ="PERMISSION_VIEW"),以描述实体BUT,当进行更新时,需要对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天全站免登陆