hhravn
hhravn

Reputation: 1741

Mapping entities to dynamic tablename

I want to create a hibernate mapping to a data-source with dynamic tablenames. The tables reflect datasets extracted from another source by a legacy tool, and the tablename itself carries meta-information about the data in the table. The tablename would change for every request.

Is it possible to map this in hibernate? I'm thinking some temporary interceptor thing to change the table name, but cant really wrap my head around the best way to do this.


edited 12/07/12

..and i probably should have mentioned that readonly access is all i need. The tables represents "report" data, hence should never be changed.

Upvotes: 0

Views: 305

Answers (1)

Firo
Firo

Reputation: 30803

dynamic table names are not so easy with hibernate because the sessionfactory is immutable. You would need to Build up a new sessionfactory everytime. Depending on the use case i see several options:

  • readonly: use an sqlquery with fixed column alias and use AliasToBean
  • r*ead write:* use xml mapping templates and build up a new sessionfactory on every request (probably slow)
  • if possible tablenames are known at startuptime: generate a mapping for each tablename and use entityname to differenciate which one to use at runtime

Upvotes: 1

Related Questions