Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.1 with Java 6 how to presist the same object to two seperate tables

 
Piece OfChum
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am upgrading a legacy application to use EJB 3.1/JPA2.0. The current application uses EJB 2.x BMP beans for persistence. I have been successful in my implementation but have run into one issue that has been a bit of a pain. The issues is that my legacy application stores an object into two separate tables. I want to know if it is possible to use the same POJO and map it slightly differently in two separate tables. Currently the application decides which table to pull the entity from based on the user type. I know this is a bogus design however the application is far to large to change this right now the plan is to eventually redesign parts of the database.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have two tables with the same schema, I would use inheritance to map them. Either a @MappedSuperclass, or TABLE_PER_CLASS inheritance.
So you would have two subclasses, one for each table, and decide which subclass to read based on the same condition you are currently using.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic