简体   繁体   English

Hibernate和具有相同数据/列但具有不同表名的表

[英]Hibernate and tables with same data/columns but with different table names

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.

Would it be possible to map these to one Java class entity with JPA and hibernate? 是否可以使用JPA和hibernate将这些映射到一个Java类实体? 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? 该类的名称将是Data ,然后在使用它时传递给例如company1 ,以便该对象将使用company1Data表?

Or is it better to just use normal, plain Java objects for something like this? 或者,为这样的事情使用普通的普通Java对象会更好吗?

Thank you! 谢谢!

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. 如果你做不到,我可以看到几种方法。

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. 您将使Data成为一个抽象实体,然后创建映射到每个表的简单具体子类。 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. 这可以工作,但是对于任何不限于特定类的查询都是低效的,因为它们将涉及查询多个表,并且使用起来会很麻烦,因为您需要为每个表使用特定的类。

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! 你可能能够在这里使用触发器做一些聪明的事情,但它将取决于你的数据库,并且比我拥有更多的数据库和JPA魔法!

In my opinion it's way more faster to you if you use Hibernate... 在我看来,如果你使用Hibernate,对你来说会更快......

The big advantage of using JPA with Hibernate is you don't need to write some sql commands, like inserts and updates. 使用JPA和Hibernate的一大优点是你不需要编写一些sql命令,比如插入和更新。

Here are some tutorials where you can start with Hibernate: 以下是一些可以从Hibernate开始的教程:

http://www.javacodegeeks.com/2012/05/tutorial-hibernate-jpa-part-1.html http://docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html http://www.javacodegeeks.com/2012/05/tutorial-hibernate-jpa-part-1.html http://docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/hibernate -gsg教程,jpa.html

Hope it helped... 希望它有所帮助......

Here are some topics with people with the same problem: 以下是一些具有相同问题的人的主题:

How to map one class to different tables using hibernate/jpa annotations 如何使用hibernate / jpa注释将一个类映射到不同的表

JPA, How to use the same class (entity) to map different tables? JPA,如何使用相同的类(实体)来映射不同的表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM