use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class StoredProcedureTest method testNamedProcWithMultipleResultSet.
public void testNamedProcWithMultipleResultSet() {
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_NAMED_RS2";
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 + "(IN PARAM1 VARCHAR(255), IN PARAM2 VARCHAR(255)) BEGIN " + "SELECT * FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1;" + "SELECT * FROM JPA_AN_ACCOUNT WHERE USERNAME = PARAM2;" + "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);
Account a = new Account();
a.setUsername("Fred");
a.setEnabled(true);
em.persist(a);
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();
LOG.info(">> Executing stored proc");
// Execute stored proc and compare
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP2");
spq.setParameter("PARAM1", "Fred");
spq.setParameter("PARAM2", "Fred");
boolean val = spq.execute();
assertTrue("Return from execute should have been true", val);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
for (Object result : results) {
LOG.info(">> result=" + result);
}
assertTrue("More results should be present but werent", spq.hasMoreResults());
List results2 = spq.getResultList();
assertNotNull("ResultSet2 was null!", results2);
assertEquals("Number of results2 was wrong", 1, results2.size());
for (Object result : results2) {
LOG.info(">> result=" + result);
}
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(Account.class);
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class CriteriaStringsTest method setUp.
/* (non-Javadoc)
* @see org.datanucleus.tests.PersistenceTestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@datanucleus.org");
p1.setAge(35);
em.persist(p1);
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@datanucleus.org");
p2.setAge(38);
p2.setBestFriend(p1);
em.persist(p2);
// 1-1 relation sample
Employee emp1 = new Employee(105, "Joe", "Bloggs", "joe.bloggs@datanucleus.org", 12000.0f, "10005");
emp1.setAge(40);
Employee emp2 = new Employee(106, "Nigel", "Bloggs", "nigel.bloggs@datanucleus.org", 13000.0f, "10006");
emp2.setAge(41);
Account acc1 = new Account();
acc1.setId(105);
acc1.setUsername("joebloggs");
acc1.setEnabled(true);
Account acc2 = new Account();
acc2.setId(106);
acc2.setUsername("nigelbloggs");
acc2.setEnabled(true);
emp1.setAccount(acc1);
emp2.setAccount(acc2);
Qualification q1 = new Qualification("BSc");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 2001);
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.DAY_OF_MONTH, 1);
q1.setDate(cal.getTime());
q1.setPerson(emp1);
Qualification q2 = new Qualification("MSc");
q2.setPerson(emp2);
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.MONTH, 4);
cal.set(Calendar.DAY_OF_MONTH, 1);
q2.setDate(cal.getTime());
em.persist(emp1);
em.persist(emp2);
em.persist(q1);
em.persist(q2);
// 1-N relation sample
Farm farm1 = new Farm("Giles Farm");
Farm farm2 = new Farm("Kiwi Farm");
Animal an1 = new Animal("Brown Cow");
Animal an2 = new Animal("Woolly Sheep");
Animal an3 = new Animal("Sheepdog");
farm1.getAnimals().add(an1);
an1.setFarm(farm1);
farm2.getAnimals().add(an2);
an2.setFarm(farm2);
farm2.getAnimals().add(an3);
an3.setFarm(farm2);
em.persist(farm1);
em.persist(farm2);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception in setUp data", e);
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations