Search in sources :

Example 1 with Account

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

the class EntityManagerTest method testFind.

/**
 * Test of EntityManager.find()
 */
public void testFind() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            em.persist(p1);
            Account acct1 = new Account();
            acct1.setUsername("fredf");
            em.persist(acct1);
            em.flush();
            Person.PK pk = p1.getPK();
            long acctId = acct1.getId();
            tx.commit();
            tx.begin();
            // Find using IdClass instance
            Person person = em.find(Person.class, pk);
            assertEquals(p1.getFirstName(), person.getFirstName());
            // Find using key value (but of slightly different type that needs conversion - DN extension)
            Account acct = em.find(Account.class, Integer.valueOf("" + acctId));
            assertEquals(acct1.getUsername(), acct.getUsername());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Account.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 2 with Account

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

the class JPQLSubqueryTest method testHavingSubquery.

/**
 * Simple query using a subquery in the HAVING clause.
 */
public void testHavingSubquery() {
    if (!(storeMgr instanceof RDBMSStoreManager)) {
        return;
    }
    DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
    if (!dba.supportsOption(DatastoreAdapter.SUBQUERY_IN_HAVING)) {
        return;
    }
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Account a1 = new Account();
            a1.setUsername("Flintstone");
            a1.setId(1);
            em.persist(a1);
            Organisation o1 = new Organisation("Flintstone");
            o1.setDescription("Freds organisation");
            em.persist(o1);
            // TODO Come up with a better sample query. Why does the JPA TCK/Spec have none?
            List<Person> result = em.createQuery("SELECT o FROM " + Organisation.class.getName() + " o " + "GROUP BY o.name HAVING EXISTS (SELECT a FROM " + Account.class.getName() + " a WHERE a.username = o.name)").getResultList();
            assertNotNull(result);
            assertEquals(1, result.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception executing query with HAVING subquery : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) Organisation(org.datanucleus.samples.annotations.models.company.Organisation) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) Person(org.datanucleus.samples.annotations.models.company.Person) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 3 with Account

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

the class GeneratedIdentityTest method testTableGenerator.

/**
 * Test of table generator.
 */
public void testTableGenerator() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        // This will create the table required by our TableGenerator specification
        Account acc1 = new Account();
        acc1.setEnabled(true);
        acc1.setUsername("bill");
        em.persist(acc1);
        em.flush();
        // Check the structure of the table
        RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
        Connection conn = (Connection) databaseMgr.getConnectionManager().getConnection(0).getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        // Check PAYMENTS table column names
        HashSet<String> columnNames = new HashSet<String>();
        columnNames.add("SEQUENCE_NAME");
        columnNames.add("NEXT_VAL");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SEQUENCE_TABLE", columnNames);
        tx.rollback();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown while checking TableGenerator table structure " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 4 with Account

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

the class CriteriaStringsTest method testCriteriaUpdate.

/**
 * Test update query using Criteria.
 */
public void testCriteriaUpdate() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        Account acc1 = new Account();
        acc1.setUsername("Joe");
        acc1.setEnabled(true);
        em.persist(acc1);
        em.flush();
        long id = acc1.getId();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaUpdate<Account> update = cb.createCriteriaUpdate(Account.class);
        Root<Account> root = update.from(Account.class);
        ParameterExpression<String> paramExpr = cb.parameter(String.class);
        update.set(root.<String>get("username"), paramExpr);
        Path idField = root.get("id");
        Predicate idEquals = cb.equal(idField, id);
        update.where(idEquals);
        Query q = em.createQuery(update);
        // change from Joe to Jim
        q.setParameter(paramExpr, "Jim");
        int numUpdated = q.executeUpdate();
        assertEquals(1, numUpdated);
        tx.rollback();
    } catch (Exception e) {
        LOG.error("Exception thrown during test", e);
        fail("Exception caught during test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager)

Example 5 with Account

use of org.datanucleus.samples.annotations.models.company.Account 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)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)7 Account (org.datanucleus.samples.annotations.models.company.Account)7 EntityManager (javax.persistence.EntityManager)6 Person (org.datanucleus.samples.annotations.models.company.Person)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)3 Connection (java.sql.Connection)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 HashSet (java.util.HashSet)1 List (java.util.List)1 StoredProcedureQuery (javax.persistence.StoredProcedureQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Path (javax.persistence.criteria.Path)1