Search in sources :

Example 1 with QueryResultMetaData

use of org.datanucleus.metadata.QueryResultMetaData 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 2 with QueryResultMetaData

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

the class XMLTest method testSqlResultSetMapping.

/**
 * Test of JPA <sql-result-set-mapping>
 */
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", 2, 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("MD_LOGIN_PLUS_ACCOUNT")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping MD_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("MD_ACCOUNT_NAMES")) {
            qrmd = md;
            break;
        }
    }
    if (qrmd == null) {
        fail("SQL ResultSet mapping MD_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.jpox.samples.one_one.unidir.Login) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) ClassMetaData(org.datanucleus.metadata.ClassMetaData) QueryResultMetaData(org.datanucleus.metadata.QueryResultMetaData)

Aggregations

ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusContext (org.datanucleus.NucleusContext)2 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)2 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)2 ClassMetaData (org.datanucleus.metadata.ClassMetaData)2 MetaDataManager (org.datanucleus.metadata.MetaDataManager)2 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)2 QueryResultMetaData (org.datanucleus.metadata.QueryResultMetaData)2 PersistentTypeMapping (org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping)2 Login (org.datanucleus.samples.annotations.one_one.unidir.Login)1 LoginAccount (org.datanucleus.samples.annotations.one_one.unidir.LoginAccount)1 Login (org.jpox.samples.one_one.unidir.Login)1 LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)1