3 Replies Latest reply on Mar 30, 2006 12:25 PM by epbernard

    Multiple Databases with Identical Schemas

      Hi,

      The company I work for has purchased some automotive industry data from a third party, this comes in the form of two databases, one for Passenger Cars and one for Light Commercial Vehicles. The schemas for the 2 databases are identical.

      Currently I have developed Entity beans for the CAR database in a car package and deployed in CAR.JAR. I have had to duplicate (copy-and-paste) these into a lcv package and deployed in LCV.JAR. Each jar has its own persistence.xml that points to the target database.

      How can I eliminate this duplication?

      How can I access the correct PersistenceContext on-the-fly?

      How can I write my Entity beans so that they can be used to query either database, without being deployed in 2 seperate jars?

        • 1. Re: Multiple Databases with Identical Schemas
          eekboom

          Hah - if I understand your problem correctly this is exactly what caused a lot of trial-and-error for me.

          After all the only thing I had to do is get my entity manager dynamically rather than having it injected:

          public EntityManager getEntityManager(String unit) {
           String path = "java:/EntityManager/" + unit;
           EntityManager entityManager = null;
           try {
           InitialContext context = new InitialContext();
           try {
           entityManager = (EntityManager) context.lookup(path);
           }
           finally {
           context.close();
           }
           }
           catch(NamingException e) {
           LOGGER.log(Level.WARNING, "Cannot retrieve entity manager: " + entityManager, e);
           }
          
           return entityManager;
           }
          

          where the "unit" parameter is the name of one of the units in your persistence.xmls.

          The entity beans don't need any special coding.

          This thread discussed the problem a while ago, but I found it more confusing than helpful:
          http://jboss.org/index.html?module=bb&op=viewtopic&t=65869&postdays=0&postorder=asc&start=0

          What finally got me going was the answer to my own question here:
          http://jboss.org/index.html?module=bb&op=viewtopic&t=79597


          • 2. Re: Multiple Databases with Identical Schemas

            Many Thanks!

            • 3. Re: Multiple Databases with Identical Schemas
              epbernard

              Actually you cna use @PersistenceContexts or @PersistenceUnits to make this code more portable.