Search in sources :

Example 11 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl 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 12 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl 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 13 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.

the class AnnotationTest method testBasic.

/**
 * Test of basic JPA annotations reading capability
 */
public void testBasic() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    // Checks for Department
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
    String prefix = cmd1.getFullClassName() + " : ";
    assertEquals(prefix + "detachable is wrong", cmd1.isDetachable(), true);
    assertEquals(prefix + "identity-type is wrong", cmd1.getIdentityType(), IdentityType.APPLICATION);
    assertEquals(prefix + "embedded-only is wrong", cmd1.isEmbeddedOnly(), false);
    assertEquals(prefix + "requires-extent is wrong", cmd1.isRequiresExtent(), true);
    assertEquals(prefix + "catalog is wrong", cmd1.getCatalog(), null);
    assertEquals(prefix + "schema is wrong", cmd1.getSchema(), null);
    assertEquals(prefix + "table is wrong", cmd1.getTable(), "JPA_AN_DEPARTMENT");
    assertEquals(prefix + "has incorrect number of persistent fields", cmd1.getNoOfManagedMembers(), 4);
    InheritanceMetaData inhmd1 = cmd1.getInheritanceMetaData();
    assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd1.getStrategy());
    // "projects"
    AbstractMemberMetaData fmd = cmd1.getMetaDataForMember("projects");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
    assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertFalse(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertTrue(prefix + "has no container specified!", fmd.getCollection() != null);
    assertEquals(prefix + "should have collection of Project elements but hasnt", fmd.getCollection().getElementType(), Project.class.getName());
    assertEquals(prefix + "shouldnt have collection of serialised elements but has", fmd.getCollection().isSerializedElement(), false);
    assertEquals(prefix + "shouldnt have collection of dependent elements but has", fmd.getCollection().isDependentElement(), false);
    // Checks for Project
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Project.class.getName(), clr);
    prefix = cmd2.getFullClassName() + " : ";
    assertEquals(prefix + "detachable is wrong", true, cmd2.isDetachable());
    assertEquals(prefix + "identity-type is wrong", cmd2.getIdentityType(), IdentityType.APPLICATION);
    assertEquals(prefix + "objectid-class is wrong", "org.datanucleus.identity.StringId", cmd2.getObjectidClass());
    assertEquals(prefix + "embedded-only is wrong", cmd2.isEmbeddedOnly(), false);
    assertEquals(prefix + "requires-extent is wrong", cmd2.isRequiresExtent(), true);
    assertEquals(prefix + "catalog is wrong", cmd2.getCatalog(), null);
    assertEquals(prefix + "schema is wrong", cmd2.getSchema(), null);
    assertEquals(prefix + "table is wrong", "JPA_AN_PROJECT", cmd2.getTable());
    assertEquals(prefix + "has incorrect number of persistent fields", cmd2.getNoOfManagedMembers(), 2);
    InheritanceMetaData inhmd2 = cmd2.getInheritanceMetaData();
    assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd2.getStrategy());
    // "name"
    fmd = cmd2.getMetaDataForMember("name");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertTrue(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
    // "budget"
    fmd = cmd2.getMetaDataForMember("budget");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertEquals(prefix + "has incorrect persistent field", fmd.getName(), "budget");
    assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) 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) InheritanceMetaData(org.datanucleus.metadata.InheritanceMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 14 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.

the class AnnotationTest method testMapKeyColumn.

/**
 * Test of persistent properties using annotations.
 */
/*public void testPersistentProperties()
    {
        NucleusContext nucleusCtx = new NucleusContext("JPA", null);
        MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);

        // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(JPAGetter.class.getName(), clr);
        assertEquals(1, cmd1.getNoOfPrimaryKeyMembers());
    }*/
/**
 * Test of column name for property instead of field
 */
/*public void testPropertyColumName()
    {
        NucleusContext nucleusCtx = new NucleusContext("JPA", null);
        MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);

        // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);

        // it is valid according JPA to have property accessor instead of field accessors. property accessors are persistent while field not.
        assertNotNull("Employee.lastName has no field information", cmd1.getMetaDataForMember("lastName"));
        assertNotNull("Employee.lastName has no column information", cmd1.getMetaDataForMember("lastName").getColumnMetaData());
        assertEquals("Employee.lastName has incorrect number of columns", 
            1, cmd1.getMetaDataForMember("lastName").getColumnMetaData().length);
        assertEquals("Employee.last has incorrect column spec", 
            "LASTNAME", cmd1.getMetaDataForMember("lastName").getColumnMetaData()[0].getName());

        ClassMetaData cmd2 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);

        // it is valid according JPA to have property accessor instead of field accessors. property accessors are persistent while field not.
        assertNotNull(cmd2.getMetaDataForMember("age"));
        assertNotNull("AGE_COL",cmd2.getMetaDataForMember("age").getColumnMetaData()[0].getName());
        assertNotNull(cmd2.getMetaDataForMember("maidenName"));
        assertEquals(FieldPersistenceModifier.NONE,cmd2.getMetaDataForMember("_maidenName").getPersistenceModifier());
        assertEquals(FieldPersistenceModifier.PERSISTENT,cmd2.getMetaDataForMember("maidenName").getPersistenceModifier());
    }*/
/**
 * Test of JPA @MapKeyColumn.
 */
public void testMapKeyColumn() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    assertEquals("phoneNumbers_key1", cmd1.getMetaDataForMember("phoneNumbers").getKeyMetaData().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) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 15 with PersistenceNucleusContextImpl

use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.

the class AnnotationTest method testByteArrayEmbeddedByDefault.

/**
 * Test of JPA Byte[] is embedded by default
 */
public void testByteArrayEmbeddedByDefault() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(ByteArray.class.getName(), clr);
    assertTrue(cmd1.getMetaDataForMember("array1").isEmbedded());
}
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)

Aggregations

PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)86 MetaDataManager (org.datanucleus.metadata.MetaDataManager)69 NucleusContext (org.datanucleus.NucleusContext)59 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)56 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 ClassMetaData (org.datanucleus.metadata.ClassMetaData)51 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)40 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)35 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)32 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)29 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)18 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)7 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)7 PackageMetaData (org.datanucleus.metadata.PackageMetaData)6 FetchPlan (org.datanucleus.FetchPlan)5 FetchPlanForClass (org.datanucleus.FetchPlanForClass)5 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)5 ArrayList (java.util.ArrayList)4