use of org.datanucleus.samples.attributeconverter.ComplicatedType in project tests by datanucleus.
the class CriteriaStringsTest method testCriteriaAttributeConverterParameter.
/**
* Test query of entity with AttributeConverter field using Criteria and a parameter of the converter type
*/
public void testCriteriaAttributeConverterParameter() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
TypeHolder h = new TypeHolder(1, "First");
h.setDetails(new ComplicatedType("FirstName", "LastName"));
em.persist(h);
em.flush();
long id = h.getId();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<TypeHolder> cq = cb.createQuery(TypeHolder.class);
Root<TypeHolder> root = cq.from(TypeHolder.class);
cq.where(cb.equal(root.get("details"), cb.parameter(ComplicatedType.class, "theDetails")));
TypedQuery q = em.createQuery(cq);
q.setParameter("theDetails", new ComplicatedType("FirstName", "LastName"));
List<TypeHolder> results = q.getResultList();
assertNotNull(results);
assertEquals(1, results.size());
TypeHolder hr = results.get(0);
assertEquals(id, hr.getId());
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.attributeconverter.ComplicatedType in project tests by datanucleus.
the class CriteriaStringsTest method testCriteriaAttributeConverterLiteral.
/**
* Test query of entity with AttributeConverter field using Criteria and a literal of the converter type
*/
public void testCriteriaAttributeConverterLiteral() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
TypeHolder h = new TypeHolder(1, "First");
h.setDetails(new ComplicatedType("FirstName", "LastName"));
em.persist(h);
em.flush();
long id = h.getId();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<TypeHolder> cq = cb.createQuery(TypeHolder.class);
Root<TypeHolder> root = cq.from(TypeHolder.class);
cq.where(cb.equal(root.get("details"), cb.literal(new ComplicatedType("FirstName", "LastName"))));
TypedQuery q = em.createQuery(cq);
List<TypeHolder> results = q.getResultList();
assertNotNull(results);
assertEquals(1, results.size());
TypeHolder hr = results.get(0);
assertEquals(id, hr.getId());
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();
}
}
Aggregations