2

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?

Sheldon Wei
  • 1,198
  • 16
  • 31
  • Hi @Shedom Wei Which JPA provider and version are you using? I'm getting an exception when trying to set up oracle with play 2.3.7. I'm using hibernate 4.3.8.Final. How your persistence.xml look like? Any help is appreciated. Thank you Shedom – rodrigoArantes Aug 14 '15 at 18:06
  • @rodrigoArantes sorry but i left the former company for a long while and i am not able to check those infomation. i think you can paster your exception here in stackoverflow. – Sheldon Wei Sep 29 '15 at 07:47

2 Answers2

3

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!

Sheldon Wei
  • 1,198
  • 16
  • 31
0

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(); 
adis
  • 5,901
  • 7
  • 51
  • 71
  • glad to hear from you. i tried as your advice on my project but both are out of work. even the second way run into compiling error. i looked up the source of `PersistenceUnit` and tried another argment `unitName`. But i cried again. what can i do, zzz. anyway thx! – Sheldon Wei Dec 06 '13 at 10:04
  • i added the annotations to `EntityManager` in service like this: `@PersistenceUnit(name="oracle") EntityManager em = JPA.em();`. it looks like work but not well. if i give the name a wrong value the `em` get nothing while give it the correct value the `em` always get from the `default` instead of `oracle`, zzz. do you have more advices, please? thx. – Sheldon Wei Dec 09 '13 at 02:55
  • Can you share your final solution. Do you have a way to do unit testing with a particular PersistenceUnit ? – Reg Mem Apr 25 '14 at 16:28