Search in sources :

Example 6 with JoinMetaData

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

the class AnnotationTest method testSecondaryTable.

/**
 * Test for use of annotations for secondary tables, in particular @SecondaryTable.
 * Uses Printer class, storing some fields in table "PRINTER" and some in "PRINTER_TONER".
 */
public void testSecondaryTable() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(Printer.class.getName(), clr);
    assertEquals("detachable is wrong", cmd.isDetachable(), true);
    assertEquals("identity-type is wrong", cmd.getIdentityType(), IdentityType.APPLICATION);
    assertEquals("embedded-only is wrong", cmd.isEmbeddedOnly(), false);
    assertEquals("requires-extent is wrong", cmd.isRequiresExtent(), true);
    assertNull("catalog is wrong", cmd.getCatalog());
    assertNull("schema is wrong", cmd.getSchema());
    assertEquals("table is wrong", cmd.getTable(), "JPA_AN_PRINTER");
    assertEquals("has incorrect number of persistent fields", cmd.getNoOfManagedMembers(), 5);
    // Check JoinMetaData at class-level
    List<JoinMetaData> joinmds = cmd.getJoinMetaData();
    assertNotNull("JoinMetaData at class-level is null!", joinmds);
    assertEquals("Number of JoinMetaData at class-level is wrong!", joinmds.size(), 1);
    assertEquals("Table of JoinMetaData at class-level is wrong", "JPA_AN_PRINTER_TONER", joinmds.get(0).getTable());
    ColumnMetaData[] joinColmds = joinmds.get(0).getColumnMetaData();
    assertEquals("Number of columns with MetaData in secondary table is incorrect", 1, joinColmds.length);
    assertEquals("Column of JoinMetaData at class-level is wrong", joinColmds[0].getName(), "PRINTER_ID");
    // "model" (stored in primary-table)
    AbstractMemberMetaData fmd = cmd.getMetaDataForMember("model");
    assertNotNull("Doesnt have required field", fmd);
    assertNull("Field 'model' has non-null table!", fmd.getTable());
    // "tonerModel" (stored in secondary-table)
    fmd = cmd.getMetaDataForMember("tonerModel");
    assertNotNull("Doesnt have required field", fmd);
    assertEquals("Field 'tonerModel' has non-null table!", fmd.getTable(), "JPA_AN_PRINTER_TONER");
}
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) JoinMetaData(org.datanucleus.metadata.JoinMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 7 with JoinMetaData

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

the class AnnotationTest method testOneToManyBiJoin.

/**
 * Test of JPA 1-N unidir FK relation.
 * Really is 1-N uni join since JPA doesnt support 1-N uni FK
 */
/*public void testOneToManyUniFK()
    {
        NucleusContext nucleusCtx = new NucleusContext(new PersistenceConfiguration(){});
        nucleusCtx.setApi("JPA");
        MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();

        // owner side
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Site.class.getName(), clr);
        AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("offices");
        assertNotNull("Site.offices is null!", fmd1);
        assertEquals("Site.offices mapped-by is incorrect", fmd1.getMappedBy(), null);
        assertEquals("Site.offices relationType is incorrect",
            fmd1.getRelationType(clr), Relation.ONE_TO_MANY_UNI);
        assertEquals("Site.offices jointable name is incorrect", fmd1.getTable(), null);
        assertNotNull("Site.offices should have join but doesnt", fmd1.getJoinMetaData());

        ElementMetaData elemmd = fmd1.getElementMetaData();
        assertNotNull("Site.offices has no element column info but should", elemmd);
        ColumnMetaData[] colmds = elemmd.getColumnMetaData();
        assertNotNull("Site.offices has incorrect element columns", colmds);
        assertEquals("Site.offices has incorrect number of element columns", colmds.length, 1);
        assertEquals("Site.offices has incorrect element column name", colmds[0].getName(), "SITE_ID");
    }*/
/**
 * Test of JPA 1-N bidir join relation
 */
public void testOneToManyBiJoin() {
    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);
    assertEquals("Manager has incorrect table name", cmd1.getTable(), "JPA_AN_MANAGER");
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("subordinates");
    assertNotNull("Manager.subordinates is null!", fmd1);
    assertEquals("Manager.subordinates mapped-by is incorrect", fmd1.getMappedBy(), "manager");
    assertEquals("Manager.subordinates relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
    assertEquals("Manager.subordinates jointable name is incorrect", fmd1.getTable(), "JPA_AN_MGR_EMPLOYEES");
    // non-owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    assertEquals("Employee has incorrect table name", cmd2.getTable(), "JPA_AN_EMPLOYEE");
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
    assertNotNull("Employee.manager is null!", fmd2);
    assertEquals("Employee.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
    assertEquals("Employee.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
    assertEquals("Employee.manager jointable name is incorrect", fmd2.getTable(), null);
    // join-table
    JoinMetaData joinmd = fmd1.getJoinMetaData();
    assertNotNull("Manager.subordinates has no join table!", joinmd);
    assertNotNull("Manager.subordinates has incorrect join columns", joinmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect owner join column name", "MGR_ID", joinmd.getColumnMetaData()[0].getName());
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNotNull("Manager.subordinates has no element column info but should", elemmd);
    assertNotNull("Manager.subordinates has incorrect element columns", elemmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of element columns", 1, elemmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect element join column name", "EMP_ID", elemmd.getColumnMetaData()[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) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 8 with JoinMetaData

use of org.datanucleus.metadata.JoinMetaData 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 9 with JoinMetaData

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

Example 10 with JoinMetaData

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

the class AnnotationTest method testMapKeyInValue.

/**
 * Test for use of map with using @Key, @Value.
 */
public void testMapKeyInValue() {
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    String prefix = cmd1.getFullClassName() + " : ";
    AbstractMemberMetaData fmd = cmd1.getMetaDataForMember("phoneNumbers");
    assertNotNull(prefix + "doesnt have required field", fmd);
    MapMetaData mapmd = fmd.getMap();
    assertNotNull(prefix + "MapMetaData is null!", mapmd);
    assertEquals(prefix + "MapMetaData keyType is incorrect", String.class.getName(), mapmd.getKeyType());
    assertEquals(prefix + "MapMetaData valueType is incorrect", PhoneNumber.class.getName(), mapmd.getValueType());
    JoinMetaData joinmd = fmd.getJoinMetaData();
    assertNull(prefix + "field \"phoneNumbers\" has join information!", joinmd);
    KeyMetaData keymd = fmd.getKeyMetaData();
    assertEquals(prefix + "field \"phoneNumbers\" has incorrect key mapped-by", "name", keymd.getMappedBy());
}
Also used : KeyMetaData(org.datanucleus.metadata.KeyMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) PhoneNumber(org.datanucleus.samples.annotations.models.company.PhoneNumber) JoinMetaData(org.datanucleus.metadata.JoinMetaData) MapMetaData(org.datanucleus.metadata.MapMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Aggregations

JoinMetaData (org.datanucleus.metadata.JoinMetaData)24 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)16 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)11 NucleusContext (org.datanucleus.NucleusContext)10 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)10 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)10 MetaDataManager (org.datanucleus.metadata.MetaDataManager)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)9 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)9 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)4 IndexMetaData (org.datanucleus.metadata.IndexMetaData)4 CollectionMetaData (org.datanucleus.metadata.CollectionMetaData)3 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)3 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)3 InvalidMetaDataException (org.datanucleus.metadata.InvalidMetaDataException)3 KeyMetaData (org.datanucleus.metadata.KeyMetaData)3 MapMetaData (org.datanucleus.metadata.MapMetaData)3 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)3