Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [gemini-dev] Gemini JPA - EntityManager question

Hello again,

I would like to have an another -quiet related- question: Why cant I do the same (putting "persistence.xml" properties from code into a Map and then create an EMF) with the eclipselink.ddl-generation property?

I would like to create an app, which first checks if tables are created (eg. as a result of drop-and-create, checking is executing a query, exception-> no tables yet), if not, it creates them and then, on the next startup it sees that tables are already created, so it just doesnt put the drop-and-create property into the EMFB.createEMF(map). //Caused by: java.sql.SQLException: Schema 'TEST' does not exist

Here is my code to help explaining:

        Map<String, Object> d = new HashMap<String, Object>();
        d.put("javax.persistence.jdbc.url","jdbc:derby:C:/temp/databases/fujDB;create=true");
        d.put("javax.persistence.jdbc.user", "test");
        d.put("javax.persistence.jdbc.password", "test");
        d.put("javax.persistence.jdbc.driver","org.apache.derby.jdbc.EmbeddedDriver");

        emf = emfb.createEntityManagerFactory(d);
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("select ep from EURProperty ep");

        try {
            q.getResultList();
            logger.debug("Database tables are already created.");
        } catch (DatabaseException e) {
            logger.debug("Database exception occured: " + e.getMessage());
            logger.info("Initializing Derby DB tables...");
           
            Map<String, Object> dd = new HashMap<String, Object>();
            dd.put("javax.persistence.jdbc.url","jdbc:derby:C:/temp/databases/ujDB;create=true");
            dd.put("javax.persistence.jdbc.user", "test");
            dd.put("javax.persistence.jdbc.password", "test");
            dd.put("javax.persistence.jdbc.driver","org.apache.derby.jdbc.EmbeddedDriver");
            dd.put("eclipselink.ddl-generation", "drop-and-create-tables");

            // Recreating EMF with dropandcreate ddl mode
            EntityManagerFactory emff = emfb.createEntityManagerFactory(d);
            EntityManager emm = emff.createEntityManager();
            Query qq = emm.createQuery("select ep from EURProperty ep");
           
            try {
                qq.getResultList();
                logger.debug("Database tables have been created.");
            } catch (DatabaseException ex) {
                logger.debug("Database exception occured: " + ex.getMessage());
                logger.error("DB tables couldnt be created. Exiting...");
                System.exit(-1);
            }
        }

Or how can I implement this functionality if not this way?

Thanks in advance,
András Liter


2011/10/6 András Liter <skarfiol@xxxxxxxxx>
Hi Mike,

thank you very much again for your quick reply, referencig the EMFB service made it work! Woohooo! :)

Best regards,
András


2011/10/6 Mike Keith <michael.keith@xxxxxxxxxx>
Hi András,

The EMFB is a registered service just like the EMF is, so if you are using DS you should be able to just reference the EMFB service instead of the EMF one. Make sense? Let me know if you have any trouble with it.

Regards,
-Mike


On 10/6/2011 12:06 PM, András Liter wrote:
Hello Mike,

thanks for your answer! I would have one more question: how can I use Gemini JPA to be able to affect EMFB by passing my property map from code?
Because In my case, when I start my bundles (Eclipse Run Configuration), I just get an EMF (not a factory builder) via Declarative Services~~Referenced Service from Equinox & Gemini JPA bundles.

Thanks in advance,
András


2011/9/29 Mike Keith <michael.keith@xxxxxxxxxx>
Hi Andras,

Yes, that is precisely what the EntityManagerFactoryBuilder is for. You can leave out the properties from the persistence.xml file, and then when you call createEntityManagerFactory() on the builder you can pass them in as properties.

-Mike

P.S. There is a forum that you can post questions to that we encourage people to use. That way others who may have the same questions can see the replies. See http://www.eclipse.org/forums/index.php/f/153


On 9/29/2011 6:00 AM, András Liter wrote:
Dear Gemini developers,

I know its a developer ml, but I just didnt find any user mail list for Gemini JPA, so I put my question here.

Is it possible to configure an EntityManager with the database URL from code, via EntityManager's EntityManager(Map m) constructor?

So far I made it work from persistence.xml, but I tried to have a Map in the constructor of EM (with only one URL property, other props stayed in persistence.xml), and get the following error during EM instantiantion:

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: URL was not specified

Do you have any ideas, how to make this "programmable" configuration work?

Thank in advance,
András Liter
_______________________________________________ gemini-dev mailing list gemini-dev@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/gemini-dev




Back to the top