use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testEmbeddedCollection.
/**
* Test for JPA embedded collection elements.
*/
public void testEmbeddedCollection() throws Exception {
addClassesToSchema(new Class[] { EmbeddedCollectionOwner.class, EmbeddedCollElement.class });
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
// Map with embedded value taking default value column names
Set<String> columnNames = new HashSet<String>();
// FK to owner
columnNames.add("JPA_COLL_EMB_OWNER_ID");
// Element "name"
columnNames.add("NAME");
// Element "value"
columnNames.add("VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_COLL_EMB", columnNames);
// Map with embedded value overriding the value column names
Set<String> columnNames2 = new HashSet<String>();
// FK to owner
columnNames2.add("JPA_COLL_EMB_OWNER_ID");
// Element "name"
columnNames2.add("COLL_ELEM_NAME");
// Element "value"
columnNames2.add("COLL_ELEM_VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_COLL_EMB_OVERRIDE", columnNames2);
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testInheritanceStrategyTablePerClass.
/**
* Test for JPA inheritance strategy "table-per-class" on a hierarchy of classes.
*/
public void testInheritanceStrategyTablePerClass() throws Exception {
addClassesToSchema(new Class[] { InheritB.class, InheritB1.class, InheritB2.class });
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
HashSet<String> columnNames = new HashSet<String>();
columnNames.add("ID");
columnNames.add("NAME");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B", columnNames);
columnNames = new HashSet<String>();
columnNames.add("ID");
columnNames.add("NAME");
columnNames.add("NAME1");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B1", columnNames);
columnNames = new HashSet<String>();
columnNames.add("ID");
columnNames.add("NAME");
columnNames.add("NAME2");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B2", columnNames);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Specification of table and column names must have been ignored when creating the schema for " + "inheritance case where the fields were overridden. Exception was thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testInheritanceStrategySingleTable.
/**
* Test for JPA inheritance strategy "single-table" on a hierarchy of classes.
*/
public void testInheritanceStrategySingleTable() throws Exception {
addClassesToSchema(new Class[] { InheritC.class, InheritC1.class, InheritC2.class });
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
HashSet<String> columnNames = new HashSet<String>();
columnNames.add("ID");
columnNames.add("NAME");
columnNames.add("NAME1");
columnNames.add("NAME2");
columnNames.add("DTYPE");
// Check base table column names
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_C", columnNames);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Specification of table and column names must have been ignored when creating the schema for " + "inheritance case where the fields were overridden. Exception was thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testMapOfSimpleSimpleViaXml.
/**
* Test for JPA Map<NonPC, NonPC> using xml.
*/
public void testMapOfSimpleSimpleViaXml() throws Exception {
addClassesToSchema(new Class[] { MapHolder1Xml.class });
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
HashSet<String> columnNames = new HashSet<String>();
columnNames.add("JPA_XML_MAPHOLDER1_ID");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1", columnNames);
HashSet<String> columnNames2 = new HashSet<String>();
columnNames2.add("MAPHOLDER1_ID");
columnNames2.add("PROP_NAME");
columnNames2.add("PROP_VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1_PROPS", columnNames2);
HashSet<String> columnNames3 = new HashSet<String>();
columnNames3.add("MAPHOLDER1XML_JPA_XML_MAPHOLDER1_ID");
columnNames3.add("PROPERTIES2_KEY");
columnNames3.add("PROPERTIES2_VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "MAPHOLDER1XML_PROPERTIES2", columnNames3);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class StoredProcedureTest method testExecuteOutputParam.
public void testExecuteOutputParam() {
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_OUTPUTPARAM";
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 + "(OUT PARAM1 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM1 FROM JPA_AN_PERSON; 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);
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();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
spq.registerStoredProcedureParameter("PARAM1", Integer.class, ParameterMode.OUT);
boolean val = spq.execute();
assertFalse("Flag for result set returned true but should have been false", val);
Object paramVal = spq.getOutputParameterValue("PARAM1");
assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
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(Person.class);
}
}
Aggregations