Search in sources :

Example 6 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method QueryJDOQL.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void QueryJDOQL() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Declarative JDOQL :
        LOGGER.log(Level.INFO, "Declarative JDOQL --------------------------------------------------------------");
        Query qDJDOQL = pm.newQuery(Product.class);
        qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
        qDJDOQL.declareParameters("double price_value");
        List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();
        Iterator<Product> iterDJDOQL = resultsqDJDOQL.iterator();
        while (iterDJDOQL.hasNext()) {
            Product p = iterDJDOQL.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 7 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class MyApp method defineDynamicPersistentUnit.

public static void defineDynamicPersistentUnit() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/jdo_db");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "root");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "admin");
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver");
    pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
    pmf = new JDOPersistenceManagerFactory(pumd, null);
    pm = pmf.getPersistenceManager();
}
Also used : JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData)

Example 8 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class JDOPersistenceTestCase method getPMF.

/**
 * Method to obtain the PMF to use allowing specification of custom user PMF properties. Creates a new PMF on each call.
 * @param userProps The custom PMF props to use when creating the PMF
 * @return The PMF (also stored in the local "pmf" variable)
 */
protected static synchronized PersistenceManagerFactory getPMF(Properties userProps) {
    if (pmf != null) {
        if (!pmf.isClosed()) {
            // Close the current PMF first
            pmf.close();
        }
    }
    pmf = JDOPersistenceTestCase.getPMF(1, userProps);
    freezePMF(pmf);
    // Set up the StoreManager
    storeMgr = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getStoreManager();
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    try {
        clr.classForName("org.datanucleus.store.rdbms.RDBMSStoreManager");
        if (storeMgr instanceof org.datanucleus.store.rdbms.RDBMSStoreManager) {
            // RDBMS datastores have a vendor id
            vendorID = ((org.datanucleus.store.rdbms.RDBMSStoreManager) storeMgr).getDatastoreAdapter().getVendorID();
        }
    } catch (ClassNotResolvedException cnre) {
    }
    return pmf;
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Example 9 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class ConvertersTest method testUseOfPersistentConverter.

/**
 * Test the use of "@Persistent(converter="...")" annotation on a field.
 */
public void testUseOfPersistentConverter() {
    try {
        PersistenceNucleusContext nucCtx = ((JDOPersistenceManagerFactory) pmf).getNucleusContext();
        ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
        AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(PersonWithConverters.class, clr);
        // Check the converter is registered with metadata
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember("myBool1");
        assertNotNull(mmd);
        assertTrue(mmd.hasExtension("type-converter-name"));
        String converterName = mmd.getValueForExtension("type-converter-name");
        assertEquals("org.datanucleus.samples.converters.Boolean10Converter", converterName);
        // Check the correct mapping is chosen for this field
        RDBMSStoreManager storeMgr = (RDBMSStoreManager) nucCtx.getStoreManager();
        DatastoreClass tbl = storeMgr.getDatastoreClass(PersonWithConverters.class.getName(), clr);
        JavaTypeMapping mapping = tbl.getMemberMapping(mmd);
        assertTrue(mapping instanceof TypeConverterMapping);
    } catch (Exception e) {
        LOG.error("Exception during test", e);
        fail("Exception was thrown : " + e.getMessage());
    } finally {
    }
}
Also used : TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) PersonWithConverters(org.datanucleus.samples.converters.PersonWithConverters) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 10 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class ConvertersTest method testUseOfConvert.

/**
 * Test the use of "@Convert" annotation on a field.
 */
public void testUseOfConvert() {
    try {
        PersistenceNucleusContext nucCtx = ((JDOPersistenceManagerFactory) pmf).getNucleusContext();
        ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
        AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(PersonWithConverters.class, clr);
        // Check the converter is registered with metadata
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember("myBool2");
        assertNotNull(mmd);
        assertTrue(mmd.hasExtension("type-converter-name"));
        String converterName = mmd.getValueForExtension("type-converter-name");
        assertEquals("org.datanucleus.samples.converters.BooleanYNConverter", converterName);
        // Check the correct mapping is chosen for this field
        RDBMSStoreManager storeMgr = (RDBMSStoreManager) nucCtx.getStoreManager();
        DatastoreClass tbl = storeMgr.getDatastoreClass(PersonWithConverters.class.getName(), clr);
        JavaTypeMapping mapping = tbl.getMemberMapping(mmd);
        assertTrue(mapping instanceof TypeConverterMapping);
    } catch (Exception e) {
        LOG.error("Exception during test", e);
        fail("Exception was thrown : " + e.getMessage());
    } finally {
    }
}
Also used : TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) PersonWithConverters(org.datanucleus.samples.converters.PersonWithConverters) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)39 PersistenceManager (javax.jdo.PersistenceManager)15 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)12 NucleusContext (org.datanucleus.NucleusContext)9 Query (javax.jdo.Query)8 MetaDataManager (org.datanucleus.metadata.MetaDataManager)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)6 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 JDOException (javax.jdo.JDOException)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 IOException (java.io.IOException)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3