Sheldon Wei
Sheldon Wei

Reputation: 1252

playframework2 how to open multi-datasource configuration with jpa

i want to configure multiple datasources in Play framework 2.1 with jpa.
one is H2, and the other is Oracle.
so i added the code like this in application.conf:


    db.default.driver=org.h2.Driver
    db.default.url="jdbc:h2:file:E:/myproject/setup/db/monitor"
    db.default.user=sa
    db.default.password=sa
    db.default.jndiName=DefaultDS
    jpa.default=defaultPersistenceUnit

    db.oracle.driver=oracle.jdbc.driver.OracleDriver
    db.oracle.url="jdbc:oracle:thin:@10.1.20.10:1521:prjct"
    db.oracle.user=LOG_ANALYSE
    db.oracle.password=LOG_ANALYSE
    db.oracle.jndiName=OracleDS
    jpa.oracle=ojdbcPersistenceUnit

i don't know how to assign for jpa.oracle and give it a meaningless name. but it does not show any errors. should i change it and how?
the main problem is: how can i tell Play which entities are managed by default datasource what others by the other, oracle? for example, class A, B's tables are in H2 and class C, D's tables are in oracle. what should i codding for these entities to assign the datasources?

Upvotes: 2

Views: 2841

Answers (2)

Sheldon Wei
Sheldon Wei

Reputation: 1252

Finally, i found the way to connect to different db sources.
in play, the api of jpa has no method named getJPAConfig("").
thers is another construction of em(), em("").
so i access the dbs as:

EntityManager em0 = JPA.em("default");     
EntityManager em1 = JPA.em("oracle"); 

that's it!

Upvotes: 3

adis
adis

Reputation: 5951

I did not used this feature (yet) but you have to useone of the annotations on your Models:

@PersistenceUnit(name="default")
@PersistenceUnit(name="oracle")

Or when you query yourself you can alsow specify it as:

EntityManager em = JPA.getJPAConfig("oracle").em(); 

Upvotes: 0

Related Questions