Search in sources :

Example 16 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class SchemaColumnTest method testColumnDefaultsStoringNullWhenNull.

/**
 * Test of default values for columns, storing null when a field is null at persist.
 */
public void testColumnDefaultsStoringNullWhenNull() {
    Properties props = new Properties();
    props.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_COLUMN_DEFAULT_WHEN_NULL, "false");
    PersistenceManagerFactory myPMF = getConfigurablePMF(1, props);
    try {
        PersistenceManager pm = myPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = new ClassWithDefaultCols(1);
            pm.makePersistent(c1);
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        myPMF.getDataStoreCache().evictAll();
        // Retrieve and check data
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = pm.getObjectById(ClassWithDefaultCols.class, 1);
            assertNull(c1.getDefaultedNameNull());
            assertNull(c1.getDefaultedName());
            assertNull(c1.getDefaultedLong());
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(myPMF, ClassWithDefaultCols.class);
        myPMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithDefaultCols(org.datanucleus.samples.schema.ClassWithDefaultCols) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Example 17 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class TypeQueryTest method testQueryWithDateJdbcTypeChange.

/**
 * Test for persistence of a java.util.Date with a jdbc-type of "DATE" and queried against
 * an input date parameter.
 */
public void testQueryWithDateJdbcTypeChange() {
    try {
        // Persist some objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Calendar cal = Calendar.getInstance();
        cal.set(2005, 5, 20);
        Date cutOffDate = cal.getTime();
        try {
            tx.begin();
            DateHolder holder1 = new DateHolder();
            Calendar cal1 = Calendar.getInstance();
            cal1.set(2003, 6, 1);
            holder1.setDateField(cal1.getTime());
            DateHolder holder2 = new DateHolder();
            Calendar cal2 = Calendar.getInstance();
            cal2.set(2006, 6, 1);
            holder2.setDateField(cal2.getTime());
            pm.makePersistent(holder1);
            pm.makePersistent(holder2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown persisting object with date field", e);
            fail("Exception thrown persisting objects : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using an input parameter
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(DateHolder.class, "dateField < :myDate");
            List results = (List) q.execute(cutOffDate);
            assertEquals("Number of objects returned from Query of date is wrong", 1, results.size());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown querying object using date parameter", e);
            fail("Exception thrown querying object using date parameter with changed jdbc-type : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(DateHolder.class);
    }
}
Also used : DateHolder(org.jpox.samples.types.basic.DateHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) List(java.util.List) Date(java.util.Date)

Example 18 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class TypesMappingTest method testMSSQLUniqueIdentifierType.

/**
 * Test for MSSQL datastore "UNIQUEIDENTIFIER" type.
 */
public void testMSSQLUniqueIdentifierType() {
    if (!vendorID.equals("sqlserver")) {
        return;
    }
    MSSQLTypes types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new MSSQLTypes();
        types.setSimpleString("some string");
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (MSSQLTypes) pm.getObjectById(id, true);
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getAnotherString().length());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new MSSQLTypes();
        types.setUuid("6F9619FF-8B86-D011-B42D-00C04FC964FF");
        types.setSimpleString("some string");
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (MSSQLTypes) pm.getObjectById(id, true);
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
        assertEquals("UUIDString retrieved is not 6F9619FF-8B86-D011-B42D-00C04FC964FF", "6F9619FF-8B86-D011-B42D-00C04FC964FF", types.getUuid());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MSSQLTypes(org.jpox.samples.rdbms.types.MSSQLTypes)

Example 19 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class TypesMappingTest method testXMLType.

/**
 * Test for Oracles XMLType.
 * TODO Need to have Oracle XMLType in CLASSPATH for this
 */
public void testXMLType() {
    if (!vendorID.equals("oracle")) {
        return;
    }
    OracleTypes types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    String xml = "<jdo><package/></jdo>";
    String xml2 = "<jdo><package name=\"org.datanucleus\"/></jdo>";
    try {
        tx.begin();
        types = new OracleTypes();
        types.setXml(xml);
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        assertEquals(xml, types.getXml());
        tx.commit();
        // test update
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        types.setXml(xml2);
        tx.commit();
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        pm.refresh(types);
        assertEquals(xml2, types.getXml());
        tx.commit();
        // test update to null
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        types.setXml(null);
        tx.commit();
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        pm.refresh(types);
        assertNull(types.getXml());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
    // test null
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new OracleTypes();
        types.setXml(null);
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        assertNull(types.getXml());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) OracleTypes(org.datanucleus.samples.dbspecific.OracleTypes)

Example 20 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class TypesMappingTest method testDB2DataLinkType.

/**
 * Test for DB2 datastore "DATALINK" type.
 */
public void testDB2DataLinkType() {
    if (!vendorID.equals("db2")) {
        return;
    }
    DB2Types types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new DB2Types();
        types.setDataLinkString("http://www.jpox.org");
        types.setDataLinkString2("http://www.someurl.org/path");
        types.setSimpleString("some string");
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (DB2Types) pm.getObjectById(id, true);
        assertEquals("DataLinkString retrieved is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
        assertEquals("DataLinkString2 retrieved is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (DB2Types) ((Collection) pm.newQuery(DB2Types.class).execute()).iterator().next();
        assertEquals("DataLinkString retrieved from Query is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
        assertEquals("DataLinkString2 retrieved from Query is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
        assertEquals("Simple String retrieved from Query is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : DB2Types(org.jpox.samples.rdbms.types.DB2Types) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65