Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA different tables, same entity-type  RSS feed

 
Per-Morten Lindanger
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty new to JPA, and I have run into a problem where we have several tables with entities of the same type.

Example:
Tables:
Itemtable2002
Itemtable2003
Itemtable2004
Itemtable2005
Itemtable2006
Itemtableybly (last year)
Itemtable (this year)

Entity for all is "Item"(same columns for all, but different data).

What I need to do, is to allow the user to choose what table to pull data from at runtime.

Any suggestions about how this can be done? The tables can not be altered.
 
Reinhard Horn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Per-Morten,

can't you put your items in one table and add a year field? It would be simpler.
You select your items with a paramerized query that takes the year as in input param.

Regards
 
Per-Morten Lindanger
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As there are loads of programs allready accessing the tables, and since there's no record regarding where thoose programs are, changing the tables are not an option.

Anyway, I solved the matter by adding an interface. It turned out looking pretty neat. Thanks for the feedback.
 
Sanjay Goaplakrishnan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
One other approach is to have a abstract entity class indicating Item and child entity classes for each of the years.
a table per entity is supported in jpa.

Please do let me know more about the 'adding an interface' approach, and how it worked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!