aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes 2 completely different data sources with EJB and JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "2 completely different data sources with EJB and JPA" Watch "2 completely different data sources with EJB and JPA" New topic
Forums: Object Relational Mapping EJB and other Java EE Technologies
Author

2 completely different data sources with EJB and JPA

Michelle Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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.

[My Blog] [JavaRanch Journal]
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

You need two entity managers, each with an explicit unitName given in the @PersistenceContext annotation.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Can anyone tell me if one EntityManager annotated with @PersistenceContexts and inside that two @PersistenceContext annotations would work as well?
Michelle Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

I notice that this thread has been requested to be marked as resolved by the OP. Michelle, did you get this working?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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

Joined: Aug 06, 2010
Posts: 543
    
    4

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 Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 77

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
Marshal

Joined: Jul 20, 2005
Posts: 10096
    
164

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

Joined: Aug 06, 2010
Posts: 543
    
    4

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2 completely different data sources with EJB and JPA