Search in sources :

Example 16 with PersistenceUnitMetaData

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

the class XMLTest method testOneToOneUni.

/**
 * Test of 1-1 uni relation
 */
public void testOneToOneUni() {
    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 cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("login");
    assertNotNull("LoginAccount.login is null!", fmd1);
    assertEquals("LoginAccount.login mapped-by is incorrect", fmd1.getMappedBy(), null);
    assertEquals("LoginAccount.login relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_ONE_UNI);
    assertNotNull("LoginAccount.login has no column info", fmd1.getColumnMetaData());
    assertEquals("LoginAccount.login has incorrect number of columns", fmd1.getColumnMetaData().length, 1);
    assertEquals("LoginAccount.login column name is wrong", fmd1.getColumnMetaData()[0].getName(), "LOGIN_ID");
}
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) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 17 with PersistenceUnitMetaData

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

the class XMLTest method testOneToManyUniJoin.

/**
 * Test of JPA 1-N unidir JoinTable relation
 */
public void testOneToManyUniJoin() {
    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(Department.class.getName(), clr);
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("projects");
    assertNotNull("Department.projects is null!", fmd1);
    assertEquals("Department.projects mapped-by is incorrect", null, fmd1.getMappedBy());
    assertEquals("Department.projects relationType is incorrect", RelationType.ONE_TO_MANY_UNI, fmd1.getRelationType(clr));
    assertEquals("Department.projects jointable name is incorrect", "JPA_MD_DEPT_PROJECTS", fmd1.getTable());
    JoinMetaData joinmd = fmd1.getJoinMetaData();
    assertNotNull("Department.projects has no join table!", joinmd);
    assertNotNull("Department.projects has incorrect join columns", joinmd.getColumnMetaData());
    assertEquals("Department.projects has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
    assertEquals("Department.projects has incorrect join column name", joinmd.getColumnMetaData()[0].getName(), "DEPT_ID");
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNotNull("Department.projects has no element column info but should", elemmd);
    ColumnMetaData[] colmds = elemmd.getColumnMetaData();
    assertNotNull("Department.projects has incorrect element columns", colmds);
    assertEquals("Department.projects has incorrect number of element columns", 1, colmds.length);
    assertEquals("Department.projects has incorrect element column name", "PROJECT_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) JoinMetaData(org.datanucleus.metadata.JoinMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 18 with PersistenceUnitMetaData

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

Example 19 with PersistenceUnitMetaData

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

the class XMLTest method testOneToOneBi.

/**
 * Test of 1-1 bi relation
 */
public void testOneToOneBi() {
    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);
    // non-owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Boiler.class.getName(), clr);
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("timer");
    assertNotNull("Boiler.timer is null!", fmd1);
    assertEquals("Boiler.timer mapped-by is incorrect", "boiler", fmd1.getMappedBy());
    assertEquals("Boiler.timer relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd1.getRelationType(clr));
    // owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Timer.class.getName(), clr);
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("boiler");
    assertNotNull("Timer.boiler is null!", fmd2);
    assertEquals("Timer.boiler mapped-by is incorrect", null, fmd2.getMappedBy());
    assertEquals("Timer.boiler relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd2.getRelationType(clr));
    assertNotNull("Timer.boiler has no column info", fmd2.getColumnMetaData());
    assertEquals("Timer.boiler has incorrect number of columns", 1, fmd2.getColumnMetaData().length);
    assertEquals("Timer.boiler column name is wrong", "BOILER_ID", fmd2.getColumnMetaData()[0].getName());
}
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) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 20 with PersistenceUnitMetaData

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

the class XMLTest method testManyToMany.

/**
 * Test of JPA M-N relation
 */
public void testManyToMany() {
    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(PetroleumCustomer.class.getName(), clr);
    assertEquals("Customer has incorrect table name", cmd1.getTable(), "JPA_MD_PETROL_CUSTOMER");
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("suppliers");
    assertNotNull("Customer.suppliers is null!", fmd1);
    assertEquals("Customer.suppliers mapped-by is incorrect", fmd1.getMappedBy(), "customers");
    assertEquals("Customer.suppliers relationType is incorrect", fmd1.getRelationType(clr), RelationType.MANY_TO_MANY_BI);
    assertEquals("Customer.suppliers jointable name is incorrect", fmd1.getTable(), "JPA_MD_PETROL_CUST_SUPP");
    // non-owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(PetroleumSupplier.class.getName(), clr);
    assertEquals("Supplier has incorrect table name", cmd2.getTable(), "JPA_MD_PETROL_SUPPLIER");
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("customers");
    assertNotNull("Supplier.customers is null!", fmd2);
    assertEquals("Supplier.customers mapped-by is incorrect", fmd2.getMappedBy(), null);
    assertEquals("Supplier.customers relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_MANY_BI);
    assertEquals("Supplier.customers jointable name is incorrect", fmd2.getTable(), null);
    // join table info
    JoinMetaData joinmd = fmd1.getJoinMetaData();
    assertNotNull("Customer.suppliers has no join table!", joinmd);
    assertNotNull("Customer.suppliers has incorrect join columns", joinmd.getColumnMetaData());
    assertEquals("Customer.suppliers has incorrect number of join columns", joinmd.getColumnMetaData().length, 1);
    assertEquals("Customer.suppliers has incorrect owner join column name", joinmd.getColumnMetaData()[0].getName(), "CUSTOMER_ID");
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNotNull("Customer.suppliers has no element column info but should", elemmd);
    assertNotNull("Customer.suppliers has incorrect element columns", elemmd.getColumnMetaData());
    assertEquals("Customer.suppliers has incorrect number of element columns", elemmd.getColumnMetaData().length, 1);
    assertEquals("Customer.suppliers has incorrect element join column name", elemmd.getColumnMetaData()[0].getName(), "SUPPLIER_ID");
}
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) JoinMetaData(org.datanucleus.metadata.JoinMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

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