Search in sources :

Example 1 with ConcreteSimpleSub1

use of org.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub1 in project tests by datanucleus.

the class AnnotationTest 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.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.datanucleus.samples.annotations.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 2 with ConcreteSimpleSub1

use of org.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub1 in project tests by datanucleus.

the class JPQLQueryTest method testQueryOfMappedSuperclass.

/**
 * Test querying of mapped superclass.
 */
public void testQueryOfMappedSuperclass() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ConcreteSimpleSub1 sub1 = new ConcreteSimpleSub1(101);
            sub1.setSub1Field("Sub1 Field");
            sub1.setBaseField("Sub1 Base Field");
            em.persist(sub1);
            em.flush();
            Query q = em.createQuery("SELECT T FROM " + AbstractSimpleBase.class.getName() + " T");
            List result = q.getResultList();
            assertEquals(1, result.size());
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ConcreteSimpleSub1.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) ConcreteSimpleSub1(org.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub1) EntityManager(javax.persistence.EntityManager) AbstractSimpleBase(org.datanucleus.samples.annotations.abstractclasses.AbstractSimpleBase) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ConcreteSimpleSub1 (org.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub1)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusContext (org.datanucleus.NucleusContext)1 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)1 JPAQuery (org.datanucleus.api.jpa.JPAQuery)1 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)1 AbstractSimpleBase (org.datanucleus.samples.annotations.abstractclasses.AbstractSimpleBase)1 ConcreteSimpleSub2 (org.datanucleus.samples.annotations.abstractclasses.ConcreteSimpleSub2)1