Search code examples
javajpaentitypersistence

Including an Entity Class programmatically with a persistence unit?


I was looking over some code that I created awhile ago, and noticed something odd.

I am creating a Persistence Unit programmatically due to needed user input as to the location of the Database to read.

My code is as follows

 Map properties = new HashMap();



            db = plan.db;


            // Configure the internal EclipseLink connection pool
            properties.put(TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
            properties.put(JDBC_DRIVER, "net.ucanaccess.jdbc.UcanaccessDriver");
            properties.put(JDBC_URL, "jdbc:ucanaccess://" + db + ";singleconnection=‌​true;memory=true");
            properties.put(JDBC_USER, "");
            properties.put(JDBC_PASSWORD, "");
          //  properties.put( "provider" , "org.eclipse.persistence.jpa.PersistenceProvider");

           EntityManagerFactory emf2;
            EntityManager em2;

            emf2 = Persistence.createEntityManagerFactory("PU", properties);
            em2 = emf2.createEntityManager();    

With this I was able to create my connections multiple times.

The problem I noticed is that I also had code in my "Persistence.xml"

 <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>db.Items</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.jdbc.url" value=""/>
  <property name="javax.persistence.jdbc.user" value=""/>
  <property name="javax.persistence.jdbc.driver" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
  <property name="javax.persistence.jdbc.password" value=""/>
</properties>

Now I noticed that I cannot find any way to add an "Entity Class" to this "Persistence Unit," however I was able to run my code fine, just like this.

I'm curious if it just overwrites the old properies and such from the Persistence Unit of the same name? It still uses the Persistence Class of "db.Items."

I just want to make sure that this is the correct way to do it.

I'm doing changes to my code, so I cannot run it currently to see if I delete everything in my PErsistence.xml what will happen, but I'm curious about this.

I also noticed that the "provider" property was commented out. Do I need that posted? (It's included in the xml file).

there is also an example I saw that mentioned about "Server target" being set to "no" or something? Any comments on that?

Thanks all


Solution

  • It overwrites the properties you have specified in persistence.xml. You can for example only set user name and password in this way and the other properties will be used as defined in the file. If it's "right" to do it this way I don't know, but I have done the same.

    The call to Persistence.createEntityManager(unit, props) starts with searching for the named unit in any persistence.xml found in the classpath. Then properties from props are added or overwritten to the properties read from file for that unit.

    I have no comment about your other questions.