Search in sources :

Example 21 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testMappedSuperclass.

/**
 * Test of MappedSuperclass
 */
public void testMappedSuperclass() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    // AbstractSimpleBase
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(AbstractSimpleBase.class.getName(), clr);
    assertNotNull("No MetaData found for AbstractSimpleBase yet is MappedSuperclass", cmd);
    assertNotNull("No Inheritance info found for AbstractSimpleBase", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for AbstractSimpleBase is incorrect", "subclass-table", cmd.getInheritanceMetaData().getStrategy().toString());
    AbstractMemberMetaData fmd = cmd.getMetaDataForMember("id");
    assertNotNull("No field info found for AbstractSimpleBase.id", fmd);
    assertNotNull("No column info found for AbstractSimpleBase.id", fmd.getColumnMetaData());
    assertEquals("Column name for AbstractSimpleBase.id is wrong", "ID", fmd.getColumnMetaData()[0].getName());
    fmd = cmd.getMetaDataForMember("baseField");
    assertNotNull("No field info found for AbstractSimpleBase.baseField", fmd);
    assertNotNull("No column info found for AbstractSimpleBase.baseField", fmd.getColumnMetaData());
    assertEquals("Column name for Product.baseField is wrong", "BASE_FIELD", fmd.getColumnMetaData()[0].getName());
    // ConcreteSimpleSub1
    cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(ConcreteSimpleSub1.class.getName(), clr);
    assertNotNull("No MetaData found for ConcreteSimpleSub1 yet is Entity", cmd);
    assertNotNull("No Inheritance info found for ConcreteSimpleSub1", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for ConcreteSimpleSub1 is incorrect", "new-table", cmd.getInheritanceMetaData().getStrategy().toString());
    fmd = cmd.getOverriddenMember("baseField");
    assertNotNull("No overridden field info found for ConcreteSimpleSub1.baseField", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub1.baseField", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub1.baseField is wrong", "BASE_FIELD_OR", fmd.getColumnMetaData()[0].getName());
    fmd = cmd.getMetaDataForMember("sub1Field");
    assertNotNull("No field info found for ConcreteSimpleSub1.sub1Field", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub1.sub1Field", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub1.sub1Field is wrong", "SUB1_FIELD", fmd.getColumnMetaData()[0].getName());
    // ConcreteSimpleSub2
    cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(ConcreteSimpleSub2.class.getName(), clr);
    assertNotNull("No MetaData found for ConcreteSimpleSub2 yet is Entity", cmd);
    assertNotNull("No Inheritance info found for ConcreteSimpleSub2", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for ConcreteSimpleSub2 is incorrect", "new-table", cmd.getInheritanceMetaData().getStrategy().toString());
    fmd = cmd.getOverriddenMember("baseField");
    assertNull("Overridden field info found for ConcreteSimpleSub2.baseField!", fmd);
    fmd = cmd.getMetaDataForMember("sub2Field");
    assertNotNull("No overridden field info found for ConcreteSimpleSub2.sub2Field", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub2.sub2Field", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub2.sub2Field is wrong", "SUB2_FIELD", fmd.getColumnMetaData()[0].getName());
}
Also used : ConcreteSimpleSub2(org.jpox.samples.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.jpox.samples.abstractclasses.ConcreteSimpleSub1) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 22 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testMapKey.

/**
 * Test of JPA <map-key>.
 */
public void testMapKey() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    assertEquals("name", cmd1.getMetaDataForMember("phoneNumbers").getKeyMetaData().getMappedBy());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 23 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class AnnotationPlusXMLTest method testMapKeyColumn.

/**
 * Test of JPA @MapKeyColumn.
 */
public void testMapKeyColumn() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    assertEquals("phoneNumbers_key1", cmd1.getMetaDataForMember("phoneNumbers").getKeyMetaData().getColumnMetaData()[0].getName());
    assertTrue(cmd1.getMetaDataForMember("phoneNumbers").getKeyMetaData().getColumnMetaData()[0].getUnique());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 24 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tutorials by eugenp.

the class MyApp method defineDynamicPersistentUnit.

public static void defineDynamicPersistentUnit() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
    pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
    pumd.addProperty("datanucleus.xml.indentSize", "4");
    pmf = new JDOPersistenceManagerFactory(pumd, null);
    pm = pmf.getPersistenceManager();
}
Also used : JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData)

Example 25 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tutorials by eugenp.

the class GuideToJDOIntegrationTest method givenProduct_WhenQueryThenExist.

@Test
public void givenProduct_WhenQueryThenExist() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addClassName("com.baeldung.jdo.Product");
    pumd.setExcludeUnlistedClasses();
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
    pumd.addProperty("datanucleus.autoCreateSchema", "true");
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Product product = new Product("Tablet", 80.0);
        pm.makePersistent(product);
        Product product2 = new Product("Phone", 20.0);
        pm.makePersistent(product2);
        Product product3 = new Product("Laptop", 200.0);
        pm.makePersistent(product3);
        tx.commit();
    } catch (Throwable thr) {
        fail("Failed test : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.close();
    PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm2 = pmf2.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx2.begin();
        @SuppressWarnings("rawtypes") Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200");
        @SuppressWarnings("unchecked") List<Product> products = (List<Product>) q.execute();
        for (Product p : products) {
            assertEquals("Laptop", p.name);
        }
        tx2.commit();
    } finally {
        if (tx2.isActive()) {
            tx2.rollback();
        }
        pm2.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) List(java.util.List) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) Test(org.junit.Test)

Aggregations

PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)48 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)40 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)40 NucleusContext (org.datanucleus.NucleusContext)39 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)38 ClassMetaData (org.datanucleus.metadata.ClassMetaData)38 MetaDataManager (org.datanucleus.metadata.MetaDataManager)38 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)23 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 JoinMetaData (org.datanucleus.metadata.JoinMetaData)9 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)6 PackageMetaData (org.datanucleus.metadata.PackageMetaData)6 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)4 EventListenerMetaData (org.datanucleus.metadata.EventListenerMetaData)3 KeyMetaData (org.datanucleus.metadata.KeyMetaData)3 MapMetaData (org.datanucleus.metadata.MapMetaData)3 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)3 TableGeneratorMetaData (org.datanucleus.metadata.TableGeneratorMetaData)3 ArrayList (java.util.ArrayList)2 PersistenceManager (javax.jdo.PersistenceManager)2