Skip to main content

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

Hi Jan,

    You can use a custom MetadataSource to collect/build the mappings for entities in jars in your application.  The default mapping file in META-INF/orm.xml is loaded automatically along with any other mapping files listed in the persistence.xml.  With EclipseLink's MetadataSource control is passed to you to load mappings from wherever you like.  You could, for example, look in your classpath for all files of a given name like META-INF/extension-orm.xml and load them.

    Shaun


On 27-02-2014 4:18 AM, Hiller, Jan wrote:

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?

 

 



_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
Oracle
Shaun Smith | Senior Principal Product Manager
Mobile: +1 416 558 6244 | Phone: +1 905 502 3094
ORACLE Canada | 100 Milverton Drive, Mississauga, Ontario | L5R 4H1

Green
          Oracle Oracle is committed to developing practices and products that help protect the environment

Back to the top