1 Reply Latest reply on Oct 20, 2005 8:00 PM by Emmanuel Bernard

    Persistence in different datasources

    Alexander Langer Newbie

      Hi!

      One of the strengths of EJB2 was that you could mix BMP and CMP entity beans and hence easily could mix different persistence layers for data storage, e.g. when migrating an application. We are in this situation: An Oracle database is filled with customer data, but we must keep customer-related data in a MySQL-database.

      I have a class Customer and a class Purchase, and they are mapped in a OneToMany relation, i.e. Purchase contains a field "customerId".
      Unfortunately, Customer's are stored in Oracle, while Purchases are stored in MySQL. (Please don't ask why, we have to do so :)

      In the EJB2.x case I can create two BMPs and just hide the OneToMany from any JOIN operations that a generic persistance layer would like to do, so I can just fill the Purchases' customerId field with the ID-field from the Oracle database and all is fine.

      Now, we want to use EJB3. I have spent my day searching how to do so with Hibernate, and obviously, you can't do so. As I understand, however, given you ignore the relations for now (just fill the customerId with an int) you can specify two EntityManager's, one of them used for Customer using a datasource for the Oracle database and Hibernate's oracle dialect, and one entity-manager for the mysql database using MySQLDialect. Is this true?

      Given that I now want to use EJB's query language, e.g. "Select all purchases of Customer c", how can I combine the the two entity managers so that I can still use this kind of query language and don't need to use something like "select all purchesas where purchase.customerid = customer.getId()" (schematic). Is this possible? If yes, how?

      I'd be very grateful if one could point me into the right direction and maybe documentation (or even better code examples).

      Thanks a lot

      Alex

        • 1. Re: Persistence in different datasources
          Emmanuel Bernard Master

           

          Now, we want to use EJB3. I have spent my day searching how to do so with Hibernate, and obviously, you can't do so. As I understand, however, given you ignore the relations for now (just fill the customerId with an int) you can specify two EntityManager's, one of them used for Customer using a datasource for the Oracle database and Hibernate's oracle dialect, and one entity-manager for the mysql database using MySQLDialect. Is this true?

          yes, 1 Persistence unit/emf per DB is the way to do it