Search in sources :

Example 41 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method persistXML.

public void persistXML() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        ProductXML productXML = new ProductXML(0, "Tablet", 80.0);
        pm.makePersistent(productXML);
        ProductXML productXML2 = new ProductXML(1, "Phone", 20.0);
        pm.makePersistent(productXML2);
        ProductXML productXML3 = new ProductXML(2, "Laptop", 200.0);
        pm.makePersistent(productXML3);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 42 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method QuerySQL.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void QuerySQL() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // SQL :
        LOGGER.log(Level.INFO, "SQL --------------------------------------------------------------");
        Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT");
        query.setClass(Product.class);
        List<Product> results = query.executeList();
        Iterator<Product> iter = results.iterator();
        while (iter.hasNext()) {
            Product p = iter.next();
            LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
        }
        LOGGER.log(Level.INFO, "--------------------------------------------------------------");
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 43 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDOIntegrationTest method givenProduct_WhenQueryThenExist.

@Test
public void givenProduct_WhenQueryThenExist() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addClassName("com.baeldung.jdo.Product");
    pumd.setExcludeUnlistedClasses();
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
    pumd.addProperty("datanucleus.autoCreateSchema", "true");
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Product product = new Product("Tablet", 80.0);
        pm.makePersistent(product);
        Product product2 = new Product("Phone", 20.0);
        pm.makePersistent(product2);
        Product product3 = new Product("Laptop", 200.0);
        pm.makePersistent(product3);
        tx.commit();
    } catch (Throwable thr) {
        fail("Failed test : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.close();
    PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm2 = pmf2.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx2.begin();
        @SuppressWarnings("rawtypes") Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200");
        @SuppressWarnings("unchecked") List<Product> products = (List<Product>) q.execute();
        for (Product p : products) {
            assertEquals("Laptop", p.name);
        }
        tx2.commit();
    } finally {
        if (tx2.isActive()) {
            tx2.rollback();
        }
        pm2.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) List(java.util.List) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) Test(org.junit.Test)

Example 44 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDOIntegrationTest method givenProduct_WhenNewThenPerformTransaction.

@Test
public void givenProduct_WhenNewThenPerformTransaction() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addClassName("com.baeldung.jdo.Product");
    pumd.setExcludeUnlistedClasses();
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
    pumd.addProperty("datanucleus.autoCreateSchema", "true");
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        for (int i = 0; i < 100; i++) {
            String nam = "Product-" + i;
            Product productx = new Product(nam, (double) i);
            pm.makePersistent(productx);
        }
        tx.commit();
    } catch (Throwable thr) {
        fail("Failed test : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.close();
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) Test(org.junit.Test)

Example 45 with PersistenceManagerFactory

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

the class PersistenceManagerFactoryTest method testServerTimeZoneID.

/**
 * Test for serverTimeZoneID setting.
 */
public void testServerTimeZoneID() {
    PersistenceManagerFactory pmf = null;
    // Try setting the serverTimeZoneID to an invalid value
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "JPOX_CENTRAL_TIMEZONE");
        pmf = getPMF(1, userProps);
        fail("Expected a JDOUserException when setting the ServerTimeZoneID to an invalid value but worked!");
    } catch (JDOFatalUserException jdoe) {
    // Expected
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try setting it to a valid value and make sure it is retained
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "UTC");
        pmf = getPMF(1, userProps);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), "UTC");
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try leaving it unset and check the value
    try {
        pmf = getPMF(1, null);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), null);
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Aggregations

PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)67 PersistenceManager (javax.jdo.PersistenceManager)52 Transaction (javax.jdo.Transaction)44 Properties (java.util.Properties)40 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)34 Employee (org.jpox.samples.models.company.Employee)18 Query (javax.jdo.Query)15 JDOUserException (javax.jdo.JDOUserException)13 Manager (org.jpox.samples.models.company.Manager)13 Iterator (java.util.Iterator)12 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)11 Extent (javax.jdo.Extent)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)10 JDOFatalUserException (javax.jdo.JDOFatalUserException)9 Collection (java.util.Collection)8 JDOException (javax.jdo.JDOException)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)8 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)8 Level2Cache (org.datanucleus.cache.Level2Cache)8 SQLException (java.sql.SQLException)7