• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

2 completely different data sources with EJB and JPA

 
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently working with EJB Session Beans on a Glassfish Service server. I have a master table in an Oracle database on one server and the detail data on a different database and for this question lets assume its on a separate server though right now they are on the same server and is currently Derby. I have had no problem setting up both databases in JDBC in Glassfish. However, I have tried all kinds of options on how to implement this master detail connection with a web service EJB. The EJB Session Bean allowed me to put them together but the persistent xml file rejected my attempt to list both databases. Guessing this is more of a JPA issue. I have created two different EJBs but couldnt figure out how to put the two databases together in a servlet. Surely this kind of situation is not something new. Would someone please help me understand the best way to implement this as web service(s) using two different databases?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need 2 persistence units (backed by a persistence.xml each) to interact with those 2 different DBs. Once you have those 2 persistence units, it's then just a matter of looking up or injecting the relevant ones in your EJBs and work with them.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to Jaikiran's answer, you don't really require two different persistence.xml files, but you need to specify two <persistence-unit> elements in your (single) persistence.xml. Like so:

Make sure you also either specify the schema in your JPA entities or <exclude-unlisted-classes>true</exclude-unlisted-classes and add <class /> elements for all JPA entities in your project.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:To add to Jaikiran's answer, you don't really require two different persistence.xml files, but you need to specify two <persistence-unit> elements in your (single) persistence.xml.



Good point. I had forgotten that's possible.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the warning I am getting "Multiple persistence units defined - only the first persistence unit will be recognized"






Also, I am getting this error when I try to publish. I am currently using only one Entity Manager.

cannot Deploy csDerbyEAR
deploy is failing=Error occurred during deployment: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.. Please see server.log for more details.
Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method : java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.
Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, would I use only one Entity Manager or should I use two? I am currently using one but when I went to publish this was the error I got: Thinking this message is more for the warning I am getting with the two persistence Units. But thought I would point this error out as well.

cannot Deploy csDerbyEAR
deploy is failing=Error occurred during deployment: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.. Please see server.log for more details.
Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method : java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.
Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.
 
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need two entity managers, each with an explicit unitName given in the @PersistenceContext annotation.
 
Rob Spoor
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me if one EntityManager annotated with @PersistenceContexts and inside that two @PersistenceContext annotations would work as well?
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Created two Entity Managers but the warning did not go away in the persistece.xml file and when I went to publish, I got the exact same error. For those who suggested the two persistence Units in the xml file, have you actually implemented this before or was this just a suggested thought? Also, Just reaching, but noticed the persistence version is 2.1. Is there a newer version which may allow this?
 
Rob Spoor
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've worked on a project with 5 or 6 persistence units in one single persistence.xml file, and that worked fine (in JBoss EAP though).
As for the persistence version, does JPA 2.2 already exist? I haven't heard of it yet.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Streeter wrote:Here is the warning I am getting "Multiple persistence units defined - only the first persistence unit will be recognized"



Are you seeing that warning on the server while deploying or are you seeing that in some IDE? Anyway, the persistence.xml XSD xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd allows more than 1 persistence-unit elements:


so whoever is complaining about it seems to be doing it wrong.


Michelle Streeter wrote:
Also, I am getting this error when I try to publish. I am currently using only one Entity Manager.

cannot Deploy csDerbyEAR
deploy is failing=Error occurred during deployment: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.. Please see server.log for more details.
Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method : java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.
Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com.cs.eao.csEAO/em] in the scope of the module called [csDerbyEAR#csDerby.jar]. Please verify your application.



When you have more than 1 persistence-unit configured in your application, the server cannot determine the "default" one to use when it see a @PersistenceContext or persistence-contex-ref in your application. So you'll have to explicitly set the unitName attribute in the @PersistenceContext annotation usage (or the DD equivalent) to point it to the persistence unit to use.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Can anyone tell me if one EntityManager annotated with @PersistenceContexts and inside that two @PersistenceContext annotations would work as well?



That won't work Rob. The PersistenceContexts annotation is a "type" level annotation and can't be used on fields or methods http://docs.oracle.com/javaee/5/api/javax/persistence/PersistenceContexts.html. The sole purpose of @PersistenceContexts is to allow binding the contained persistence contexts to the environment naming context (ENC) of the component. No injection is done using @PersistenceContexts.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that this thread has been requested to be marked as resolved by the OP. Michelle, did you get this working?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:
As for the persistence version, does JPA 2.2 already exist? I haven't heard of it yet.



The latest is JPA 2.1 which is part of Java EE 7.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The eclipse ide is who is complaining about the persistence.xml as for marking it resolved, it probably is for all tense and purposes. I believe the answers are correct, Now I need to figure out how to make it work. Does that make sense? Should I have waited to mark it that way until I figure it out too?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Streeter wrote:as for marking it resolved, it probably is for all tense and purposes. I believe the answers are correct, Now I need to figure out how to make it work. Does that make sense? Should I have waited to mark it that way until I figure it out too?



That's fine. I just wanted to be sure that it was intentional and your questions had been answered.

 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:
When you have more than 1 persistence-unit configured in your application, the server cannot determine the "default" one to use when it see a @PersistenceContext or persistence-contex-ref in your application. So you'll have to explicitly set the unitName attribute in the @PersistenceContext annotation usage (or the DD equivalent) to point it to the persistence unit to use.



Would you give me an example of this? I am using an Interface for my Entity classes which implement generic persist, find or fail, remove, and flush methods in the EAO. And since an Entity Manager is a bean, I created a parent EAO and then two children EAOs with extend the parent where the Entity Managers reside. Is this a good way of doing this? Here is my code: I have not done a total clean up.







 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the tutorial I used to learn about JPA.

JEE Tutorial

The tutorial gave me some really good stuff but I didnt have to learn the basics to arrive here and so because of this, I am missing stuff which explains why I am not seeing how to deviate from the tutorials setup.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, here is my attempt at trying to come up with something that might work. It actually publishes but when I ask for people, I am getting a null pointer error. See previous message as to why I am doing hit and miss programming here.









 
Rob Spoor
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Streeter wrote:So, here is my attempt at trying to come up with something that might work. It actually publishes but when I ask for people, I am getting a null pointer error. See previous message as to why I am doing hit and miss programming here.


The entity manager is not injected because you haven't told Glassfish to do so. Add @PersistenceContext(unitName=...) to it, like you've already done for the other. As I said before, you need an explicit persistence unit name (as you have done for your other bean).
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I needed the csEAO to be generic for the conversion and transactionrollbacks. Thats why I didnt put on in the csEAO. I am guessing I dont understand something.

I went ahead and specified the csDerby for the csEAO in hopes that when I use the csOracleEAO it will know to use the right one. And it did. It works. Thank You.

Thanks to all the people who helped me with this.

BTW, I have my degree in CS, I have been programming for over 35 years. My last job was as a hardware design engineer. Also some embedded software. Not sure why but I have been jobless for the last two years and have been homeless for the last year. I am living in a horse trailer but have been teaching myself JEE stuff to pass the time.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use delegation, not inheritance, and inject your persistence context in cseao using the unit name.

I have done this many times btw. There's nothing to it. You just have to realize that the server needs to be clear on which persistence context you want to use.

but SSBs extending SSBs, that's just asking for trouble.
 
Michelle Nicholes
Ranch Hand
Posts: 167
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those of you who have done this before: And Now that I have the two databases working in one EJB. Is it better to modify the Entities to join them or just call the detail data for each iteration of the master table? Can you join them with each table being in a separate EM?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michelle Streeter wrote:Can you join them with each table being in a separate EM?



That won't be possible.

Michelle Streeter wrote:or just call the detail data for each iteration of the master table?



That's the way you'll have to do it.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Jaikiran. However, if you do want to have the necessary data in one class (rich domain model etc) nothing is stopping you from performing a JNDI lookup in your master entity and populating the necessary @Transient fields on @PostLoad. Whether you can do this eagerly or not depends on the possible size of your resultset though. You don't want to ruin your performance.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic