public void testXMLCustomerOrder() {
// skip if dictionary has no support for XML column type
if (!enabled)
return;
OpenJPAEntityManagerSPI em = emf.createEntityManager();
DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
.getDBDictionaryInstance();
String sqllog = TestXMLCustomerOrder.class.getName();
sqllog = sqllog.replace('.', '/');
sqllog = "./" + sqllog;
if (dict instanceof DB2Dictionary)
sqllog += ".db2";
else if (dict instanceof OracleDictionary)
sqllog += ".oracle";
else if (dict instanceof SQLServerDictionary)
sqllog += ".sqlserver";
// For platform specific expected sqls are under resources.
// The generated sql of the test is captured and written to file:
// ./TestXMLCustomerOrder.log
// This output file contents should match with the platform specfic
// sqls.
System.out.println("Expected pushdown SQL log file is in: " + sqllog);
sql.clear();
try {
em.getTransaction().begin();
deleteAllData(em );
em.getTransaction().commit();
em.getTransaction().begin();
loadData(em);
em.getTransaction().commit();
em.close();
// By closing and recreating the EntityManager,
// this guarantees that data will be retrieved from
// the database rather than just reused from the
// persistence context created by the load methods above.
em = emf.createEntityManager();
System.err.println("Main started.");
int test=1;
List<Address> addrs = em.createQuery(
"select o.shipAddress from Order o")
.getResultList();
for (Address addr : addrs) {
System.out.println("addr= " + addr.toString());
}
String qstrings[] = {
"select o from Order o",
"select o from Order o, Order o2 where o.shipAddress.city " +
"= o2.shipAddress.city",
"select o from Order o, Customer c where o.shipAddress.city " +
"= c.address.city",
"select o from Order o where o.shipAddress.city = 'San Jose'"
};
String qstring = null;
for (int i = 0;i < qstrings.length; i++) {
qstring = qstrings[i];
List orders = em.createQuery(qstring).getResultList();
printOrders(orders, test++);
}
// query passing parameters
qstring = "select o from Order o where o.shipAddress.city = ?1";
Query q5 = em.createQuery(qstring);
q5.setParameter(1, "San Jose");
List orders =q5.getResultList();
printOrders(orders, test++);
qstring = "select o from Order o where ?1 = o.shipAddress.city";
Query q6 = em.createQuery(qstring);
q6.setParameter(1, "San Jose");
orders = q6.getResultList();
printOrders(orders, test++);
em.close();
// test updates
em = emf.createEntityManager();
testUpdateShipaddress(em, test++);
em.close();
em = emf.createEntityManager();
// query after updates
orders = em.createQuery("select o from Order o").getResultList();
System.out.println("After Update:");
printOrders(orders, test++);
// queries expecting exceptions
String[] badqstrings = {
"select o from Order o where o.shipAddress.city = 95141",
"select o from Order o where o.shipAddress.street " +
"= '555 Bailey'",
"select o from Order o where o.shipAddress.zip = 95141"
};
for (int i = 0; i < badqstrings.length; i++) {
qstring = badqstrings[i];
try {
System.out.println("\n>> Query "+test+": "+qstring);
test++;
orders = em.createQuery(qstring).getResultList();
}
catch (Exception e){
System.out.println("Exception: "+e);
}
}
dumpSql();
em.close();
emf.close();
System.out.println("Main ended normally.");
} catch (Exception e){
System.out.println("Exception: "+e);
e.printStackTrace();