Search in sources :

Example 16 with MetaDataManager

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

the class AnnotationTest method testCharDefaultTo1Length.

/**
 * Test of char length default to 1 with JPA.
 */
public void testCharDefaultTo1Length() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(TypeHolder.class.getName(), clr);
    assertEquals(1, cmd1.getMetaDataForMember("char1").getColumnMetaData()[0].getLength().intValue());
}
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) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 17 with MetaDataManager

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

the class AnnotationTest method testOneToManyBiFK.

/**
 * Test of JPA 1-N bidir FK relation
 */
public void testOneToManyBiFK() {
    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);
    // owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("departments");
    assertNotNull("Manager.departments is null!", fmd1);
    assertEquals("Manager.departments mapped-by is incorrect", fmd1.getMappedBy(), "manager");
    assertEquals("Manager.departments relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNull("Manager.departments has join column info but shouldnt (specified on N side)", elemmd);
    // non-owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
    assertNotNull("Department.manager is null!", fmd2);
    assertEquals("Department.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
    assertEquals("Department.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
    ColumnMetaData[] colmds = fmd2.getColumnMetaData();
    assertNotNull("Department.manager has no join column info", colmds);
    assertEquals("Department.manager has incorrect number of joincolumns", colmds.length, 1);
    assertEquals("Department.manager joincolumn name is wrong", "MGR_ID", colmds[0].getName());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) ElementMetaData(org.datanucleus.metadata.ElementMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 18 with MetaDataManager

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

the class AnnotationTest method testSqlResultSetMapping.

/**
 * Test of JPA @SqlResultSetMapping
 */
public void testSqlResultSetMapping() {
    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);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
    assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
    assertEquals("LoginAccount has incorrect number of query result mappings", 4, queryResultMappings.length);
    // Example 1 : Returning 2 entities
    QueryResultMetaData qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("AN_LOGIN_PLUS_ACCOUNT")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping AN_LOGIN_PLUS_ACCOUNT is not present!");
    }
    String[] scalarCols = qrmd.getScalarColumns();
    assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
    assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
    // LoginAccount
    assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
    assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
    // Login
    assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
    assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
    // Example 2 : Returning 2 scalars
    qrmd = null;
    for (int i = 0; i < queryResultMappings.length; i++) {
        QueryResultMetaData md = queryResultMappings[i];
        if (md.getName().equals("AN_ACCOUNT_NAMES")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping AN_ACCOUNT_NAMES is not present!");
    }
    scalarCols = qrmd.getScalarColumns();
    assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
    assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
    assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
    sqlMappingEntities = qrmd.getPersistentTypeMappings();
    assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
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) Login(org.datanucleus.samples.annotations.one_one.unidir.Login) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) ClassMetaData(org.datanucleus.metadata.ClassMetaData) QueryResultMetaData(org.datanucleus.metadata.QueryResultMetaData)

Example 19 with MetaDataManager

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

the class AnnotationTest method testStringLength.

/**
 * Test of string length default to JPA default 255.
 */
public void testStringLength() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Account.class.getName(), clr);
    AbstractMemberMetaData mmd1 = cmd1.getMetaDataForMember("username");
    assertEquals(255, mmd1.getColumnMetaData()[0].getLength().intValue());
}
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) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 20 with MetaDataManager

use of org.datanucleus.metadata.MetaDataManager 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)

Aggregations

MetaDataManager (org.datanucleus.metadata.MetaDataManager)102 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)72 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)69 NucleusContext (org.datanucleus.NucleusContext)65 ClassMetaData (org.datanucleus.metadata.ClassMetaData)55 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)40 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)38 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)31 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)19 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 ArrayList (java.util.ArrayList)11 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)9 PackageMetaData (org.datanucleus.metadata.PackageMetaData)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 FetchPlanForClass (org.datanucleus.FetchPlanForClass)6 FileMetaData (org.datanucleus.metadata.FileMetaData)6