| /******************************************************************************* |
| * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * dclarke - initial |
| ******************************************************************************/ |
| package example; |
| |
| import java.lang.reflect.Field; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Properties; |
| |
| import javax.persistence.EntityManagerFactory; |
| |
| import org.eclipse.persistence.config.PersistenceUnitProperties; |
| import org.eclipse.persistence.dynamic.DynamicClassLoader; |
| import org.eclipse.persistence.dynamic.DynamicType; |
| import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl; |
| import org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl; |
| import org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo; |
| import org.eclipse.persistence.internal.sessions.DatabaseSessionImpl; |
| import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper; |
| import org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder; |
| import org.eclipse.persistence.jpa.rs.PersistenceFactoryBase; |
| import org.eclipse.persistence.jpa.rs.service.JPARSPersistenceContextFactoryProvider; |
| import org.eclipse.persistence.logging.SessionLog; |
| import org.eclipse.persistence.sessions.factories.SessionManager; |
| import org.eclipse.persistence.tools.schemaframework.SchemaManager; |
| |
| public class CreateDataService { |
| |
| public static void setup() { |
| SessionManager.getManager().destroyAllSessions(); |
| |
| // Create a dynamic class loader and create the types. |
| DynamicClassLoader dcl = new DynamicClassLoader(Thread.currentThread().getContextClassLoader()); |
| |
| Class<?> personClass = dcl.createDynamicClass("model.Person"); |
| JPADynamicTypeBuilder person = new JPADynamicTypeBuilder(personClass, null, "D_PERSON"); |
| person.setPrimaryKeyFields("P_ID"); |
| person.addDirectMapping("id", int.class, "P_ID"); |
| person.addDirectMapping("name", String.class, "NAME"); |
| person.configureSequencing("PERSON_SEQ", "P_ID"); |
| |
| DynamicType[] types = new DynamicType[] { person.getType() }; |
| |
| // Create an entity manager factory. |
| EntityManagerFactory emf = createEntityManagerFactory(dcl, true); |
| |
| // Create JPA Dynamic Helper (with the emf above) and after the types |
| // have been created and add the types through the helper. |
| JPADynamicHelper helper = new JPADynamicHelper(emf); |
| helper.addTypes(true, true, types); |
| |
| // Create database and populate |
| new SchemaManager(helper.getSession()).replaceDefaultTables(); |
| |
| } |
| |
| private static EntityManagerFactory createEntityManagerFactory(DynamicClassLoader dcl, boolean createTables) { |
| Map<String, Object> props = new HashMap<String, Object>(); |
| |
| // Ensure the persistence.xml provided data source are ignored for Java |
| // SE testing |
| props.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ""); |
| props.put(PersistenceUnitProperties.JTA_DATASOURCE, ""); |
| |
| if (createTables) { |
| props.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE); |
| props.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION); |
| } |
| |
| // Configure the use of embedded derby for the tests allowing system |
| // properties of the same name to override |
| props.put(PersistenceUnitProperties.JDBC_DRIVER, "org.apache.derby.jdbc.EmbeddedDriver"); |
| props.put(PersistenceUnitProperties.JDBC_URL, "jdbc:derby:target/derby/dynamic-api;create=true"); |
| props.put(PersistenceUnitProperties.JDBC_USER, "app"); |
| props.put(PersistenceUnitProperties.JDBC_PASSWORD, "app"); |
| props.put(PersistenceUnitProperties.CLASSLOADER, dcl); |
| props.put(PersistenceUnitProperties.WEAVING, "static"); |
| props.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINE_LABEL); |
| |
| SEPersistenceUnitInfo info = new SEPersistenceUnitInfo(); |
| info.setClassLoader(dcl); |
| info.setPersistenceUnitName("test"); |
| Properties p = new Properties(); |
| p.putAll(props); |
| info.setProperties(p); |
| |
| EntityManagerSetupImpl setup = new EntityManagerSetupImpl("test", "test"); |
| setup.predeploy(info, props); |
| DatabaseSessionImpl sessionImpl = setup.deploy(dcl, props); |
| EntityManagerFactoryImpl emf = new EntityManagerFactoryImpl(sessionImpl); |
| |
| try { |
| Field field = JPARSPersistenceContextFactoryProvider.class.getDeclaredField("factory"); |
| field.setAccessible(true); |
| Object object = field.get(JPARSPersistenceContextFactoryProvider.class); |
| System.out.println(">" + object); |
| |
| PersistenceFactoryBase factory = (PersistenceFactoryBase) object; |
| factory.bootstrapPersistenceContext("test", emf, null, "v1.0", true); |
| } catch (NoSuchFieldException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (SecurityException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (IllegalArgumentException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (IllegalAccessException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| |
| return emf; |
| } |
| |
| } |