| Change table name at runtime [message #537481] |
Wed, 02 June 2010 11:16  |
Marc Fargas Messages: 22 Registered: May 2010 |
Junior Member |
|
|
Hi there,
I've got a bunch of .DBF files that share the same structure, say they're named SALES200X.DBF and then I've got a Java class:
@Table("SALES2001");
class Sale { ...}
Then, I'd like to be able to work with other years, so I'd need to change the Table Name EclipseLink goes for the data at runtime.
I've being googling around without luck, the best I found was about messing with class loaders...
Is there someway I could modify the Table Name for a class at runtime? (ie: when creating the EntityManagerFactory).
Cheers,
Marc
|
|
|
| Re: Change table name at runtime [message #537714 is a reply to message #537481] |
Thu, 03 June 2010 09:03   |
James Sutherland Messages: 1844 Registered: July 2009 |
Senior Member |
|
|
You can change the name of a classes table using the descriptor API.
ClassDescriptor.setTablesNames() (or getTables().clear(), setTableName())
You can access the descriptors using a SessionCustomizer or DescriptorCustomizer or through accessing the EclipseLink Session from the EntityManager.
You can only change the tables until the session is initialized/connected.
You can also add new descriptors at runtime, but can only have one descriptor per class per session.
You might consider having a subclass of Sale per year, or dynamically define a new session/EntityManagerFactory for each year, or dynamically generate a subclass and descriptor per year. You may wish to investigate EclipseLink's dynamic support.
James : Wiki : Book : Blog
|
|
|
|
|
| Re: Change table name at runtime [message #538485 is a reply to message #537481] |
Mon, 07 June 2010 13:59   |
James Sutherland Messages: 1844 Registered: July 2009 |
Senior Member |
|
|
Don't worry about sessions.xml, it is not used with JPA.
You can get a Session from an EclipseLink JpaEntityManager using getSession(), or using em.unwrap(Session.class).
You can add a SessionCustomizer to your persistence.xml using the "eclipselink.session.customizer" persistence unit property.
James : Wiki : Book : Blog
|
|
|
|
|
|
| Re: Change table name at runtime [message #540918 is a reply to message #537481] |
Thu, 17 June 2010 11:45   |
James Sutherland Messages: 1844 Registered: July 2009 |
Senior Member |
|
|
As I said, you should not change a descriptor's table names after login. It should be done before creating your EntityManagerFactory, such as in a SessionCustomizer. Since the descriptors are shared across all EntityManagers, changing them on the fly is a bad idea.
You could unprepare the descriptor's insertQuery (from its DescriptorQueryManager) to get the insert to reprepare the SQL with the new table name. But changing descriptors on the fly is not a good idea.
You could try creating an entire new EntityManagerFactory on the fly with the new descriptor, or create a subclass for each table and clone the descriptor to map the subclass with the new table, or investigate EclipseLink dynamic Entity support.
James : Wiki : Book : Blog
|
|
|
|
Powered by
FUDForum. Page generated in 0.02274 seconds