Search in sources :

Example 1 with Project

use of org.datanucleus.samples.annotations.models.company.Project 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 2 with Project

use of org.datanucleus.samples.annotations.models.company.Project in project tests by datanucleus.

the class EmbeddedTest method testOneToManyWithEmbeddedId.

/**
 * Test of 1-N relation when the owner has an embedded id.
 */
public void testOneToManyWithEmbeddedId() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Department dept = new Department("Marketing");
            DepartmentPK deptPK = new DepartmentPK(101, "Mkt");
            dept.setPrimaryKey(deptPK);
            Project prj1 = new Project("DN 2.0", 100000);
            dept.getProjects().add(prj1);
            em.persist(dept);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown creating data", e);
            fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Query q = em.createQuery("SELECT d FROM " + Department.class.getName() + " d");
            List<Department> depts = q.getResultList();
            assertNotNull("Returned Department List is null", depts);
            assertEquals("Number of Departments is incorrect", 1, depts.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception thrown retrieving data", e);
            fail("Exception thrown while retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Department.class);
        clean(Project.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Project(org.datanucleus.samples.annotations.models.company.Project) EntityManager(javax.persistence.EntityManager) Department(org.datanucleus.samples.annotations.models.company.Department) Query(javax.persistence.Query) DepartmentPK(org.datanucleus.samples.annotations.models.company.DepartmentPK)

Example 3 with Project

use of org.datanucleus.samples.annotations.models.company.Project in project tests by datanucleus.

the class JPQLQueryTest method testJoinRootOn.

/**
 * Test use of JPQL JOIN to another root using ON (DataNucleus Extension).
 */
public void testJoinRootOn() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Project prj1 = new Project("DataNucleus", 1000000);
            em.persist(prj1);
            Project prj2 = new Project("JPOX", 50000);
            em.persist(prj2);
            Account acct1 = new Account();
            acct1.setUsername("DataNucleus");
            acct1.setId(1);
            em.persist(acct1);
            em.flush();
            /**
             *This will generate the following
             *
             *QueryCompilation:
             *  [result:PrimaryExpression{p.name},PrimaryExpression{p.budget}]
             *  [from:ClassExpression(alias=p join=JoinExpression{JOIN_INNER PrimaryExpression{Account} alias=a on=DyadicExpression{PrimaryExpression{p.name}  =  PrimaryExpression{a.username}}})]
             *  [symbols: p type=org.datanucleus.samples.annotations.models.company.Project, a type=org.datanucleus.samples.annotations.models.company.Account]
             *
             *SELECT P."NAME",P.BUDGET FROM JPA_AN_PROJECT P INNER JOIN JPA_AN_ACCOUNT A ON P."NAME" = A.USERNAME
             */
            Query q = em.createQuery("SELECT p.name, p.budget FROM " + Project.class.getName() + " p JOIN Account a ON p.name = a.username");
            List<Object[]> results = q.getResultList();
            assertNotNull(results);
            assertEquals(1, results.size());
            for (Object[] row : results) {
                assertEquals(2, row.length);
                assertEquals("DataNucleus", row[0]);
                assertEquals(new Long(1000000), row[1]);
            }
            // TODO Add some asserts, or choose a good example for this
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Account.class);
        clean(Project.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Project(org.datanucleus.samples.annotations.models.company.Project) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery)

Example 4 with Project

use of org.datanucleus.samples.annotations.models.company.Project in project tests by datanucleus.

the class EntityManagerTest method testMergeOfTransientAsAttach.

/**
 * Test of EntityManager.merge() of a transient object that represents one in the datastore
 */
public void testMergeOfTransientAsAttach() {
    try {
        EntityManager em = emf.createEntityManager();
        em.setProperty(PropertyNames.PROPERTY_ALLOW_ATTACH_OF_TRANSIENT, "true");
        EntityTransaction tx = em.getTransaction();
        try {
            // Persist an object
            tx.begin();
            Project p1 = new Project("DataNucleus Xenon", 125000);
            em.persist(p1);
            tx.commit();
            // Merge the detached object
            Project p2 = new Project("DataNucleus Xenon", 150000);
            tx.begin();
            Project p = em.merge(p2);
            assertEquals("Project budget not merged in returned object", 150000, p.getBudget());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown on merge of transient", e);
            fail("Exception using merge of transient " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
        emf.getCache().evictAll();
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            Project p = (Project) em.find(Project.class, "DataNucleus Xenon");
            assertEquals("Budget is incorrect. Merge didn't succeed", 150000, p.getBudget());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown on retrieval", e);
            fail("Exception using retrieval " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
    } finally {
        clean(Project.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Project(org.datanucleus.samples.annotations.models.company.Project) EntityManager(javax.persistence.EntityManager) EntityExistsException(javax.persistence.EntityExistsException) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Aggregations

Project (org.datanucleus.samples.annotations.models.company.Project)4 EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 Query (javax.persistence.Query)2 EntityExistsException (javax.persistence.EntityExistsException)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceException (javax.persistence.PersistenceException)1 TypedQuery (javax.persistence.TypedQuery)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)1 NucleusContext (org.datanucleus.NucleusContext)1 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)1 JPAQuery (org.datanucleus.api.jpa.JPAQuery)1 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 InheritanceMetaData (org.datanucleus.metadata.InheritanceMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 Account (org.datanucleus.samples.annotations.models.company.Account)1 Department (org.datanucleus.samples.annotations.models.company.Department)1