休眠和具有相同数据/列但表名称不同的表 [英] Hibernate and tables with same data/columns but with different table names

查看:66
本文介绍了休眠和具有相同数据/列但表名称不同的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的数据库中有许多表具有相同的列,但是(显然)表名称不同(我没有设计)。例如(这些是数据库表名称):

The database I am working with has many tables with the same columns but with (obviously) different table names (I didn't design it). For example (these are database table names):

company1Data
company2Data
company3Data
etc.

是否可以使用JPA将它们映射到一个Java类实体并休眠?该类的名称为 Data ,然后在使用它时将例如 company1 传递给某个地方,这样该对象将使用 company1Data 表?

Would it be possible to map these to one Java class entity with JPA and hibernate? The name of the class would be Data, and then pass in for example company1 somewhere when using it so that object would use the company1Data table?

还是仅将普通的普通Java对象用于这种情况更好?

Or is it better to just use normal, plain Java objects for something like this?

谢谢!

推荐答案

基本上,这里的问题是错误的(非常可怕的)数据库设计。理想情况下,您只需解决此问题即可。

Fundamentally, the problem here is a bad (really, terrible) database design. Ideally, you would simply fix this.

如果您做不到,我可以看到几种方法。

If you can't, I can see a couple of approaches.

一种方法是使用每个类的映射以继承为模型。您可以将 Data 用作抽象实体,然后创建映射到每个表的琐碎的具体子类。这是可行的,但对于不受限于特定类的任何查询而言,效率低下,因为它们将涉及查询多个表,并且将难以使用,因为您需要为每个表使用特定的类。

One is to model this with inheritance, using a table per class mapping. You would make Data an abstract entity, then create trivial concrete subclasses mapped to each table. This would work, but would be inefficient for any queries not constrained to a particular class, because they will involve querying multiple tables, and will be awkward to use, because you need to work with a specific class for each table.

另一种方法是使用视图修复此问题。创建一个视图,它是所有表的并集,然后将其映射。这里的问题是您将无法插入其中。您也许可以在这里使用触发器做一些聪明的事情,但这将取决于您的数据库,并且比我拥有更多的数据库和JPA向导!

Another is to fix this with a view. Create a view which is the union of all the tables, and then map that. The problem here is that you won't be able to insert into it. You might be able to do something clever with triggers here, but it will depend on your database, and take more database and JPA wizardry than i have!

这篇关于休眠和具有相同数据/列但表名称不同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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