JPA映射视图和表的继承 [英] JPA mapping views and tables with inheritance

查看:85
本文介绍了JPA映射视图和表的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库设计,我不能改变。数据库具有表和视图,它们具有一对一的关系。这些视图包含从表格中计算出的一些额外信息。相关信息是该行的状态。为这种关系设置的权限是视图是只读的,因为表格具有所有可用的CRUD操作。 JPA是选定的映射到此设置的ORM。该应用程序是一个基本的CRUD应用程序,通过检查视图中的状态和其他属性进行验证,然后插入/更新相应的表格。以下是我尝试对这种情况建模的两个认可的例子。我想知道哪一个更有效率,更容易处理,和/或'正确的方式'来做到这一点。



选项1 - 这个选项很好,因为我可以使用所有JPA提供的接口与数据库进行交互。这个选项是不好的,因为我必须加载一个表格和一个看起来非常冗余的模型,并且每个表格有3个文件。

  Model.java 
包模型;
//相关导入
公共抽象类模型{
//与表和视图共享的列
}

Table.java
包装款式;
//相关导入
@entity(table)
public class Table extends Model {
//与其他表的关系
}

View.java
包模型;
//相关导入
@entity(view)
public class View extends Model {
//查看特定列...
//与其他视图

$ / code>

选项2 - 这个选项很好,因为我只需要加载查看和我有一个文件每个表。这个选项不好,因为对于CUD操作,我必须编写原生SQL。

  Model.java 
包模型;
//相关导入
@entity(view)
public class Model {
//所有VIEW +表列
//与其他模型的所有关系
//自定义SQL插入更新或删除


解决方案

如果所有表都具有相应的访问权限,那么我会在模型对象内指定您的视图实体作为一对一关系查看对象。你可以通过编写没有setter的getter来做到这一点,因为解雇任何类型的设置然后保存将运行一个失败的查询。使用像这样的继承会锁定您必须在一个级别指定所有列,并且您不知道哪些列属于哪些表或视图。

  Table.java 
包模型;
//相关导入
@entity(table)
public class Table {
@OneToOne(mappedBy =table)
private查看视图;
public string getVariable();
public string setVaraible();
}

View.java
包模型;
//相关导入
@entity(view)
public class View {
@OneToOne
@JoinColumn(name =table_id)
私人表格表;

public string getVariable();
public string getVariable2();
public string getVariable3(); //等,没有setters。

//或者在所有列注释中使用insertable // updateable = false
@Column(name =variable_4,insertable = false,updateable = false)
public string getVariable4 ();

$ / code>

在模型对象中将它们集合在一起排序使得具有ORM在那里,因为现在你将不得不编写大量的mysql代码来匹配ORM的基本CRUD功能。这将是你的尽头。



如果您选择稍后使用它,则在此处不使用不公平的继承将作为实际选项打开。每次加入视图可能会对性能造成不良影响,这取决于您的视图写得如何,当然,如果不将它们全部放在同一个对象中,则可以在此方面获得更大的灵活性。


I have a db design that I cannot change. The db has tables and view which have a one to one relationship. The views hold some extra information that is calculated from the table. The relevant information being the status of the row. The permission set up for this relationship is views are read-only where as the tables have all CRUD operations available. JPA was the selected ORM to map to this setup. The app is a basic CRUD app with some validation by checking statuses and other attributes in the view then inserting/updating the corresponding tables. Here is an example of two approaces I have tried to model this case. I was wondering which one is more efficient, easier to work with, and/or the 'correct way' to do this.

Option 1 - This options is nice because I can use all the JPA provided interfaces to interact with the DB. This options is bad because I have to sometimes load a table and a model which seems very redundant and I have 3 files per table.

    Model.java
    package models;
    // relevant imports
    public abstract class Model {
       // columns that are shared with table and view
    }

    Table.java
    package models;
    // relevant imports
    @entity("table")
    public class Table extends Model {
       // Relationships with other tables 
    }

    View.java
    package models;
    // relevant imports
    @entity("view")
    public class View extends Model {
       // View specific columns ...
       // Relationships with other views
    }

Option 2 - This option is nice because I only ever have to load the view and I have one file per table. This option is bad because for the CUD operations I have to write native SQL.

    Model.java
    package models;
    // relevant imports
    @entity("view")
    public class Model {
       // All VIEW + table columns
       // All relationships with other models
       // custom SQL to insert update or delete
    }

解决方案

I'd specify your view entity as a one to one relationship inside your model object with read only access, if all of your tables all have a corresponding view object. You could do this just by writing getters with no setters, as firing any sort of set then saving will run a failing query. Using inheritance like this would lock you in to having to specify all your columns in one level, and you won't know which columns belong to which tables or views.

    Table.java
    package models;
    // relevant imports
    @entity("table")
    public class Table{
        @OneToOne(mappedBy = "table")
        private View view;
        public string getVariable();
        public string setVaraible();
    }

    View.java
    package models;
    // relevant imports
    @entity("view")
    public class View{
       @OneToOne
       @JoinColumn(name = "table_id")
       private Table table;

       public string getVariable();
       public string getVariable2();
       public string getVariable3();//etc, No setters.

       //alternatively use insertable//updateable=false on all column annotation
       @Column(name="variable_4", insertable =  false, updateable=false)
       public string getVariable4();
    }

Lumping them all together in the model object sort of defeats the object of having the ORM there in the first place, because now you will have to write alot of mysql code to match the ORM's basic CRUD functionality. This would be redundnacy on your end.

Not using inhertiance here leaves inheritance open as an actual option should you choose to use it later down the line. Joining to the view every time may be bad for performance depending on how well your views are written of course, but not having them all in the same object allows for more flexibility in that sense.

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

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