Search in sources :

Example 6 with Account

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

the class StoredProcedureTest method testNamedProcWithMultipleResultSet.

public void testNamedProcWithMultipleResultSet() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_NAMED_RS2";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255), IN PARAM2 VARCHAR(255)) BEGIN " + "SELECT * FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1;" + "SELECT * FROM JPA_AN_ACCOUNT WHERE USERNAME = PARAM2;" + "END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            Account a = new Account();
            a.setUsername("Fred");
            a.setEnabled(true);
            em.persist(a);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            LOG.info(">> Executing stored proc");
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP2");
            spq.setParameter("PARAM1", "Fred");
            spq.setParameter("PARAM2", "Fred");
            boolean val = spq.execute();
            assertTrue("Return from execute should have been true", val);
            List results = spq.getResultList();
            assertNotNull("ResultSet was null!", results);
            assertEquals("Number of results was wrong", 1, results.size());
            for (Object result : results) {
                LOG.info(">> result=" + result);
            }
            assertTrue("More results should be present but werent", spq.hasMoreResults());
            List results2 = spq.getResultList();
            assertNotNull("ResultSet2 was null!", results2);
            assertEquals("Number of results2 was wrong", 1, results2.size());
            for (Object result : results2) {
                LOG.info(">> result=" + result);
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Account.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) List(java.util.List) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 7 with Account

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

the class CriteriaStringsTest method setUp.

/* (non-Javadoc)
     * @see org.datanucleus.tests.PersistenceTestCase#setUp()
     */
@Override
protected void setUp() throws Exception {
    super.setUp();
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@datanucleus.org");
        p1.setAge(35);
        em.persist(p1);
        Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@datanucleus.org");
        p2.setAge(38);
        p2.setBestFriend(p1);
        em.persist(p2);
        // 1-1 relation sample
        Employee emp1 = new Employee(105, "Joe", "Bloggs", "joe.bloggs@datanucleus.org", 12000.0f, "10005");
        emp1.setAge(40);
        Employee emp2 = new Employee(106, "Nigel", "Bloggs", "nigel.bloggs@datanucleus.org", 13000.0f, "10006");
        emp2.setAge(41);
        Account acc1 = new Account();
        acc1.setId(105);
        acc1.setUsername("joebloggs");
        acc1.setEnabled(true);
        Account acc2 = new Account();
        acc2.setId(106);
        acc2.setUsername("nigelbloggs");
        acc2.setEnabled(true);
        emp1.setAccount(acc1);
        emp2.setAccount(acc2);
        Qualification q1 = new Qualification("BSc");
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, 2001);
        cal.set(Calendar.MONTH, 5);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        q1.setDate(cal.getTime());
        q1.setPerson(emp1);
        Qualification q2 = new Qualification("MSc");
        q2.setPerson(emp2);
        cal.set(Calendar.YEAR, 2011);
        cal.set(Calendar.MONTH, 4);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        q2.setDate(cal.getTime());
        em.persist(emp1);
        em.persist(emp2);
        em.persist(q1);
        em.persist(q2);
        // 1-N relation sample
        Farm farm1 = new Farm("Giles Farm");
        Farm farm2 = new Farm("Kiwi Farm");
        Animal an1 = new Animal("Brown Cow");
        Animal an2 = new Animal("Woolly Sheep");
        Animal an3 = new Animal("Sheepdog");
        farm1.getAnimals().add(an1);
        an1.setFarm(farm1);
        farm2.getAnimals().add(an2);
        an2.setFarm(farm2);
        farm2.getAnimals().add(an3);
        an3.setFarm(farm2);
        em.persist(farm1);
        em.persist(farm2);
        tx.commit();
    } catch (Exception e) {
        LOG.error(">> Exception in setUp data", e);
    } 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) Qualification(org.datanucleus.samples.annotations.models.company.Qualification) Employee(org.datanucleus.samples.annotations.models.company.Employee) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) Person(org.datanucleus.samples.annotations.models.company.Person)

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