Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 329008 - Support dynamic context creation without persistence.xml
Summary: Support dynamic context creation without persistence.xml
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Guy Pelletier CLA
QA Contact:
URL:
Whiteboard: submitted_patch
Keywords:
Depends on: 225321
Blocks:
  Show dependency tree
 
Reported: 2010-10-29 04:22 EDT by Doug Clarke CLA
Modified: 2022-06-09 10:02 EDT (History)
4 users (show)

See Also:


Attachments
Patch of JPA component on 2.1 branch (50.33 KB, patch)
2010-10-29 04:33 EDT, Doug Clarke CLA
no flags Details | Diff
Proposed changes (19.55 KB, patch)
2010-11-17 09:20 EST, Guy Pelletier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Doug Clarke CLA 2010-10-29 04:22:10 EDT
Add support for application bootstrap creation of a JPA persistence unit/context without requiring a persistence.xml file. This will allow both static classes to be dynamically mapped.

Here is a simple example:

        Map<String, Object> properties = new HashMap<String, Object>();

        // Configure the use of EclipseLink's native connection pool
        properties.put(JDBC_DRIVER, "com.mysql.jdbc.Driver");
        properties.put(JDBC_URL, "jdbc:mysql://localhost:3306/test");
        properties.put(JDBC_USER, "root");
        properties.put(JDBC_PASSWORD, "password");
        properties.put(JDBC_READ_CONNECTIONS_MIN, "1");
        properties.put(JDBC_WRITE_CONNECTIONS_MIN, "1");
        properties.put(LOGGING_LEVEL, "FINE");

        // Create Descriptors for persistent types
        List<ClassDescriptor> descriptors = new ArrayList<ClassDescriptor>();

        RelationalDescriptor empDesc = new RelationalDescriptor();
        empDesc.setJavaClass(Employee.class);
        empDesc.setAlias("Employee");
        empDesc.setTableName("EMPLOYEE");
        empDesc.addPrimaryKeyFieldName("EMP_ID");
        empDesc.addDirectMapping("id", "EMP_ID");
        empDesc.addDirectMapping("firstName", "F_NAME");
        empDesc.addDirectMapping("lastName", "L_NAME");

        descriptors.add(empDesc);

        EntityManagerFactory emf = ZeroConfigPersistence.createEntityManagerFactory("test", properties, descriptors);

        EntityManager em = emf.createEntityManager();

        List<Employee> results = em.createQuery("SELECT e FROM Employee e").getResultList();

        for (Employee result : results) {
            System.out.println("> " + result);
        }

        em.close();
        emf.close();

The ZeroConfigPersistence's static method is written as:

    public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map<String, Object> properties, List<ClassDescriptor> descriptors) {

        // Manually create a PersistenceUnitInfo
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        SEPersistenceUnitInfo info = new SEPersistenceUnitInfo();
        info.setClassLoader(loader);
        info.setNewTempClassLoader(loader);
        info.setPersistenceUnitName(persistenceUnitName);
        info.getProperties().put(PersistenceUnitProperties.WEAVING, "false");
        
        // Bug 225321 requires a non-null root URL
        try {
            info.setPersistenceUnitRootUrl(new File(".").toURI().toURL());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // This replaces Persistence.createEntityManagerFactory
        EntityManagerSetupImpl setup = new EntityManagerSetupImpl();
        setup.predeploy(info, null);
        setup.getSession().addDescriptors(descriptors);

        return new EntityManagerFactoryImpl(setup, properties);
    }

This functionality needs to be added and more easily accessible.
Comment 1 Doug Clarke CLA 2010-10-29 04:33:40 EDT
Created attachment 182021 [details]
Patch of JPA component on 2.1 branch

Simple additional constructor on EntityManagerFactoryImpl 

Also includes dependent bug's proposed fixes.
Comment 2 Guy Pelletier CLA 2010-11-17 09:20:24 EST
Created attachment 183293 [details]
Proposed changes

Updated patch
Comment 3 Guy Pelletier CLA 2010-11-17 10:26:06 EST
Changes have been submitted.

Reviewed by: Andrei Ilitchev

New test (testDynamicWithNoPersistenceXML) added to EntityMappingsDynamicAdvancedJUnitTestCase.java
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:02:54 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink