Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 329008

Summary: Support dynamic context creation without persistence.xml
Product: z_Archived Reporter: Doug Clarke <douglas.clarke>
Component: EclipselinkAssignee: Guy Pelletier <guy.pelletier>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: douglas.clarke, eclipselink.foundation-inbox, guy.pelletier, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: submitted_patch
Bug Depends on: 225321    
Bug Blocks:    
Attachments:
Description Flags
Patch of JPA component on 2.1 branch
none
Proposed changes none

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