Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] JPA Adding entity classes dynamically at runtime - Issue

Hi,

 

I’m using EclipseLink 2.5.1 in a JavaSE Application.

 

I have the requirement to add entityclasses from jars to my persistence unit dynamically at runtime. I want to be able

to simply put new jars into a folder in the class path and load the entity classes by name.

 

This is the Main

 

public class App {

 

    private static EntityManagerFactory emf;

 

    public static void main(String[] args) {

 

        loginToDB();

       

        Person person = new Person();

        person.setName("Spiderman");

       

        Customer customer = new Customer();

        customer.setName("Superman");

       

        EntityManager em = emf.createEntityManager();       

        em.getTransaction().begin();

       

        em.persist(person);

        em.persist(customer);

       

        em.getTransaction().commit();

        em.close();

       

    }

 

    public static void loginToDB() {

        Map propertyMap = new HashMap();

        propertyMap.put("javax.persistence.jdbc.user", "TestUser");

        propertyMap.put("javax.persistence.jdbc.password", "pw");

        emf = Persistence.createEntityManagerFactory("db", propertyMap);       

    }

}

 

And here the persistence.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="db" transaction-type="RESOURCE_LOCAL">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    <properties>

      <property name="eclipselink.canonicalmodel.subpackage" value="one"/>

      <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>

      <property name="eclipselink.jdbc.batch-writing.size" value="1000"/>

      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Database"/>

      <property name="javax.persistence.jdbc.password" value="pw"/>

      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>

      <property name="javax.persistence.jdbc.user" value="TestUser"/>

      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

    </properties>

  </persistence-unit>

</persistence>

 

 

The Class Person is in the MainProject and detected by “<exclude-unlisted-classes>false</exclude-unlisted-classes>” while the Customer class is in an other Project as Dependency and NOT detected. Is there any solution to add the customer class programmatically to the persistence unit?

 

 


Back to the top