2

I am writing a program which will display different translations of a book depending on the selection. so suppose I have 3 tables with same columns in DB let say, English, French and German. Then I have a POJO, say Book.java.

Now how can I use that same class to query the specific table based on user selection. Do I change the value of @table(name="...") dynamically? is that even possible? I have been using jdbc for a while but this will be my first hibernate project.

Rizwan
  • 97
  • 1
  • 12

2 Answers2

6

You could store all the fields and methods in a base class annotated with @MappedSuperclass, and have three different subclasses, each having a different @Table annotation value.

But it seems like a normalization problem to me. If you have three tables holding eactly the same columns, why not using a single table with an additional language column?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Agreed, you should normalize. If size becomes an issue, shard/partition the table based on language. Having separate tables (or even separate columns...) for each language is going to trip you up - what happens when the table structure changes? – Clockwork-Muse Aug 23 '11 at 20:50
  • Thanks guys but I can't modify DB, I also wasn't looking for hierarchy. I ended up using hbm.xml's entity-name attribute. Now I can use just one class for all kind of translations. – Rizwan Aug 29 '11 at 16:45
1

You should design a class Hierarchy : Book class beeing the topmost class, BookFrench, BookGerman, BookEnglish beeing subclasses. Then you will have to use one of the hibernate inheritance model (for that, I presume MappedSuperClass would be the most interesting one). Then you will have different classes but all instances will be book Pojos and each subclass will have its own table in DB.

Regards, stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173