blob: 88cccf7244321ac5899f23d64359a603d901661c [file] [log] [blame]
/*******************************************************************************
* 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 test.api;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.dynamic.DynamicType;
import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper;
import org.eclipse.persistence.script.EntityType;
import org.eclipse.persistence.script.PersistenceUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestCreatePersistenceUnit {
@Test
public void verifyEmf() {
Assert.assertNotNull(emf);
}
@Test
public void createPerson() {
EntityManager em = emf.createEntityManager();
try {
JPADynamicHelper helper = new JPADynamicHelper(em);
DynamicType personType = helper.getType("Person");
DynamicEntity person = personType.newDynamicEntity();
person.set("name", "Test");
em.getTransaction().begin();
em.persist(person);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
private static EntityManagerFactory emf;
@BeforeClass
public static void setup() {
emf = createPersonPU("test", PersistenceUnitProperties.DROP_AND_CREATE);
}
@AfterClass
public static void tearDown() {
emf.close();
}
public static EntityManagerFactory createPersonPU(String name, String schemaGen) {
PersistenceUnit pu = new PersistenceUnit(name);
pu.setDriver("org.apache.derby.jdbc.EmbeddedDriver");
pu.setUrl("jdbc:derby:target/derby/"+name+ ";create=true");
pu.setUserName("app");
pu.setPassword("app");
EntityType person = pu.addType("Person", "D_PERSON");
person.generatedId("id", int.class, "P_ID");
person.basic("name", String.class, "NAME");
EntityType address = pu.addType("Address", "D_Address");
address.generatedId("id", int.class, "P_ID");
address.basic("street", String.class, "NAME");
return pu.create(schemaGen);
}
}