aspose file tools*
The moose likes Object Relational Mapping and the fly likes one Entity for two Database Schemas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one Entity for two Database Schemas" Watch "one Entity for two Database Schemas" New topic
Author

one Entity for two Database Schemas

Ast Lavond
Greenhorn

Joined: Mar 02, 2010
Posts: 16
Hi,

is it possible to use the same Entity for two Database Schemas?
Both Schemas ("schema" and "schemaHistory") are identical. The Database Schema "schema" contains all current data and "schemaHistory" all the data, which were archived.

Width JDBC, it’s no problem. But I like to use the defined Entities.
Can you tell the Entity Manager which Schema and DataSource he should use?
The @Table(schema=”…”) annotation is probably not needed?

Thanks!
lanvond

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You could use two different persistence units and use XML for both or one to change the schema.
There is a default schema in the orm.xml, potentially you could use the same annotation and just give a mostly empty orm.xml with a different default schema.

Otherwise you may be able to use some vendor specific property if your JPA provider supports this.

If using EclipseLink you can set the tableQualifier for one persistence unit.

EclipseLink also supports history, so if enabled will maintain this history table for you and allow historical queries into it.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Ast Lavond
Greenhorn

Joined: Mar 02, 2010
Posts: 16
…Many thanks for the information.
I use the reference implementation from jBoss as persistence provider.
A seam application runs on this Application Server. It’s a CRUD Application and users have the option to search for data in the database schema “xy”.

Now I would like to implement an extended search → user can also search for archived data on schema “xyHistory”. This is done at runtime.
The results are two Lists. List A contains the data from schema “xy” and List B contains the data from “xyHistory”.
In JDBC I would open two database connections….

Unfortunately I’m not as well familiar with JPA.
You could use two different persistence units and use XML for both or one to change the schema.


Can I switch to the other schema with persistence units at runtime?
It’s not enough to use a second entity manager?

There is a default schema in the orm.xml, potentially you could use the same annotation and just give a mostly empty orm.xml with a different default schema.


Sorry, I don’t understand this. At the moment there isn’t a orm.xml in my Project.
An example code would be very helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one Entity for two Database Schemas