5

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? 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?

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

Thank you!

Julien Bodin
  • 783
  • 3
  • 19
shiruba
  • 51
  • 1
  • 3
  • What is the structure of the tables? Given an object, how would you be able to tell which table to store it in? – Tom Anderson May 23 '13 at 07:38
  • They have an id of a company infront, so f.ex. 1010Data, 9034Data, etc. and I know the id. "INSERT INTO" + id + "data.....". – shiruba May 23 '13 at 07:41
  • Do you mean the names of the tables have IDs in front? – Tom Anderson May 23 '13 at 07:42
  • Exactly, not the id of the table itself, but the id of a company, that is mapped (company, companyid) in another table. So company with id 1010 has a data table that is named "1010data", company with id 9034 has a data table that is named "9034data", etc. (and it is many, but the structure of each one is the same). Don't want to create a new class for each one. – shiruba May 23 '13 at 07:49

2 Answers2

1

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. 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!

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • Yes, it is terrible. I guess it's either table inheritance (there would be so many classes then though) or just creating a normal Java class that is the same as the table(s) and then use those between the business logic and DAO. I looked a bit on hibernate NamingStrategy but am not sure if I could use it... – shiruba May 23 '13 at 08:49
  • @shiruba: Sounds like you've done your homework. You are in an unfortunate situation, and you have my sympathies. Sadly, JPA was designed to allow a sane mapping of a sane database schema, not to bring sanity to an insane database schema! – Tom Anderson May 23 '13 at 10:09
0

In my opinion it's way more faster to you if you use Hibernate...

The big advantage of using JPA with Hibernate is you don't need to write some sql commands, like inserts and updates.

Here are some tutorials where you can start with 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

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

JPA, How to use the same class (entity) to map different tables?

Community
  • 1
  • 1
Manuel Pires
  • 637
  • 3
  • 19
  • Thank you. I know how to use hibernate, but what I'm wondering is if I have several database tables that have identical columns, can I use one entity class for all of them? @Entity @Table(name="???") – shiruba May 23 '13 at 07:29
  • Yes, I checked them out. Might not be possible then...Thanks for your help. – shiruba May 23 '13 at 07:42