Search in sources :

Example 1 with ComplicatedType

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();
    }
}
Also used : TypeHolder(org.datanucleus.samples.attributeconverter.TypeHolder) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) ComplicatedType(org.datanucleus.samples.attributeconverter.ComplicatedType) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery)

Example 2 with ComplicatedType

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();
    }
}
Also used : TypeHolder(org.datanucleus.samples.attributeconverter.TypeHolder) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) ComplicatedType(org.datanucleus.samples.attributeconverter.ComplicatedType) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery)

Aggregations

EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 TypedQuery (javax.persistence.TypedQuery)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 ComplicatedType (org.datanucleus.samples.attributeconverter.ComplicatedType)2 TypeHolder (org.datanucleus.samples.attributeconverter.TypeHolder)2