use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class EntityManagerTest method testFind.
/**
* Test of EntityManager.find()
*/
public void testFind() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
em.persist(p1);
Account acct1 = new Account();
acct1.setUsername("fredf");
em.persist(acct1);
em.flush();
Person.PK pk = p1.getPK();
long acctId = acct1.getId();
tx.commit();
tx.begin();
// Find using IdClass instance
Person person = em.find(Person.class, pk);
assertEquals(p1.getFirstName(), person.getFirstName());
// Find using key value (but of slightly different type that needs conversion - DN extension)
Account acct = em.find(Account.class, Integer.valueOf("" + acctId));
assertEquals(acct1.getUsername(), acct.getUsername());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Account.class);
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class JPQLSubqueryTest method testHavingSubquery.
/**
* Simple query using a subquery in the HAVING clause.
*/
public void testHavingSubquery() {
if (!(storeMgr instanceof RDBMSStoreManager)) {
return;
}
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.SUBQUERY_IN_HAVING)) {
return;
}
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Account a1 = new Account();
a1.setUsername("Flintstone");
a1.setId(1);
em.persist(a1);
Organisation o1 = new Organisation("Flintstone");
o1.setDescription("Freds organisation");
em.persist(o1);
// TODO Come up with a better sample query. Why does the JPA TCK/Spec have none?
List<Person> result = em.createQuery("SELECT o FROM " + Organisation.class.getName() + " o " + "GROUP BY o.name HAVING EXISTS (SELECT a FROM " + Account.class.getName() + " a WHERE a.username = o.name)").getResultList();
assertNotNull(result);
assertEquals(1, result.size());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in query", e);
fail("Exception executing query with HAVING subquery : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class GeneratedIdentityTest method testTableGenerator.
/**
* Test of table generator.
*/
public void testTableGenerator() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
// This will create the table required by our TableGenerator specification
Account acc1 = new Account();
acc1.setEnabled(true);
acc1.setUsername("bill");
em.persist(acc1);
em.flush();
// Check the structure of the table
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = (Connection) databaseMgr.getConnectionManager().getConnection(0).getConnection();
DatabaseMetaData dmd = conn.getMetaData();
// Check PAYMENTS table column names
HashSet<String> columnNames = new HashSet<String>();
columnNames.add("SEQUENCE_NAME");
columnNames.add("NEXT_VAL");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SEQUENCE_TABLE", columnNames);
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while checking TableGenerator table structure " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class CriteriaStringsTest method testCriteriaUpdate.
/**
* Test update query using Criteria.
*/
public void testCriteriaUpdate() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Account acc1 = new Account();
acc1.setUsername("Joe");
acc1.setEnabled(true);
em.persist(acc1);
em.flush();
long id = acc1.getId();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaUpdate<Account> update = cb.createCriteriaUpdate(Account.class);
Root<Account> root = update.from(Account.class);
ParameterExpression<String> paramExpr = cb.parameter(String.class);
update.set(root.<String>get("username"), paramExpr);
Path idField = root.get("id");
Predicate idEquals = cb.equal(idField, id);
update.where(idEquals);
Query q = em.createQuery(update);
// change from Joe to Jim
q.setParameter(paramExpr, "Jim");
int numUpdated = q.executeUpdate();
assertEquals(1, numUpdated);
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception caught during test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.annotations.models.company.Account in project tests by datanucleus.
the class JPQLQueryTest method testJoinRootOn.
/**
* Test use of JPQL JOIN to another root using ON (DataNucleus Extension).
*/
public void testJoinRootOn() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Project prj1 = new Project("DataNucleus", 1000000);
em.persist(prj1);
Project prj2 = new Project("JPOX", 50000);
em.persist(prj2);
Account acct1 = new Account();
acct1.setUsername("DataNucleus");
acct1.setId(1);
em.persist(acct1);
em.flush();
/**
*This will generate the following
*
*QueryCompilation:
* [result:PrimaryExpression{p.name},PrimaryExpression{p.budget}]
* [from:ClassExpression(alias=p join=JoinExpression{JOIN_INNER PrimaryExpression{Account} alias=a on=DyadicExpression{PrimaryExpression{p.name} = PrimaryExpression{a.username}}})]
* [symbols: p type=org.datanucleus.samples.annotations.models.company.Project, a type=org.datanucleus.samples.annotations.models.company.Account]
*
*SELECT P."NAME",P.BUDGET FROM JPA_AN_PROJECT P INNER JOIN JPA_AN_ACCOUNT A ON P."NAME" = A.USERNAME
*/
Query q = em.createQuery("SELECT p.name, p.budget FROM " + Project.class.getName() + " p JOIN Account a ON p.name = a.username");
List<Object[]> results = q.getResultList();
assertNotNull(results);
assertEquals(1, results.size());
for (Object[] row : results) {
assertEquals(2, row.length);
assertEquals("DataNucleus", row[0]);
assertEquals(new Long(1000000), row[1]);
}
// TODO Add some asserts, or choose a good example for this
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Account.class);
clean(Project.class);
}
}
Aggregations