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();
}
}
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();
}
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;
}
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 {
}
}
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 {
}
}
Aggregations