Skip to main content

Table in different schema with JPA

2 replies [Last post]
p3p0
Offline
Joined: 2009-06-30

Hi friends. Currently I am using Oracle and I am working on MY_DATA schema and everyhing is ok using JPA; but now I need access to a table in OTHER_DATA schema , the table is called OTHER_DATA.CUSTOMER:

How do I can see that table using JPA and working with the tables in MY_DATA schema?

A lot of thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
enderfake
Offline
Joined: 2006-09-06

Well if you are using eclipselink as your Persistence Provider you can switch tables, schemas, and sequence @ID generators using the following as a sort of runtime dynamic JPA:

entitymanager= emf.createEntityManager();
session = ((JpaEntityManager)entitymanager.getDelegate()).getActiveSession();

String tablename=session.getActiveSession().getClassDescriptor(YourBean.class).getTableName();
String schema=session.getActiveSession().getClassDescriptor(YourBean.class).getDefaultTable().getTableQualifier();
String column=session.getActiveSession().getClassDescriptor(YourBean.class).getMappingForAttributeName("somefield").getField().getName();
String sequence_name=session.getActiveSession().getClassDescriptor(YourBean.class).getSequence().getName();
so if you want to change the table or schema for a specific JPA entity to a new table/schema, do the following....

session.getActiveSession().getClassDescriptor(YourBean.class).getDefaultTable().setName("new_table_name");
session.getActiveSession().getClassDescriptor(YourBean.class).getDefaultTable().setTableQualifier("schema_name");

Then do your normal JPA stuff...
entitymanager.getTransaction().begin();
Query query = entitymanager.createQuery("select e from Yourbean e");
List result = query.getResultList();

To add eclipselink, download the eclipselink jar, add it to your project and add
org.eclipse.persistence.jpa.PersistenceProvider to your persistence.xml file.

Dominik Dorn

@Entity
@Table(name = "module", schema = "core_pmm")
public class Module implements Serializable {

On Mon, Mar 22, 2010 at 8:58 PM, wrote:
> Hi friends. Currently I am using Oracle and I am working on MY_DATA schema and everyhing is ok using JPA; but now I need access to a table in OTHER_DATA schema , the table is called OTHER_DATA[b].[/b]CUSTOMER:
>
> How do I can see that table using JPA and working with the tables in MY_DATA schema?
>
> A lot of thanks.
> [Message sent by forum member 'p3p0']
>
> http://forums.java.net/jive/thread.jspa?messageID=393190
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

--
[url=http://www.dominikdorn.com]Dominik Dorn[/url]

Tausche Deine [url=http://www.studyguru.eu] Lernunterlagen [/url] auf
http://www.studyguru.eu !

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net