Search in sources :

Example 1 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtils method createEntityFromTuple.

// /// Private helper methods
private static <T> List<T> createEntityFromTuple(List<Tuple> tuples, Class<T> entity) {
    List<T> entities = new ArrayList<>();
    for (Tuple t : tuples) {
        T el;
        try {
            el = entity.getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new AssertionError();
        }
        for (TupleElement<?> te : t.getElements()) {
            Object o = t.get(te);
            try {
                Field f = getFieldFromEntity(entity, te.getAlias());
                f.setAccessible(true);
                f.set(el, o);
            } catch (NoSuchFieldException | IllegalAccessException e1) {
                throw new NoSuchEntityFieldException(e1.getMessage(), te.getAlias(), entity.getSimpleName());
            }
        }
        entities.add(el);
    }
    return entities;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) CriteriaField(com.github.tfaga.lynx.beans.CriteriaField) Field(java.lang.reflect.Field) Tuple(javax.persistence.Tuple) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)

Example 2 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtilsOrderTest method testCaseSensitiveField.

@Test
public void testCaseSensitiveField() {
    QueryOrder qo = new QueryOrder();
    qo.setField("firsTNAmE");
    qo.setOrder(OrderDirection.ASC);
    QueryParameters q = new QueryParameters();
    q.getOrder().add(qo);
    try {
        JPAUtils.queryEntities(em, User.class, q);
        Assert.fail("No exception was thrown");
    } catch (NoSuchEntityFieldException e) {
        Assert.assertEquals("firsTNAmE", e.getField());
    }
}
Also used : QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) Test(org.junit.Test)

Example 3 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtilsOrderTest method testNonExistentColumn.

@Test
public void testNonExistentColumn() {
    QueryOrder qo = new QueryOrder();
    qo.setField("lstnm");
    qo.setOrder(OrderDirection.DESC);
    QueryParameters q = new QueryParameters();
    q.getOrder().add(qo);
    try {
        JPAUtils.queryEntities(em, User.class, q);
        Assert.fail("No exception was thrown");
    } catch (NoSuchEntityFieldException e) {
        Assert.assertEquals("lstnm", e.getField());
    }
}
Also used : QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) Test(org.junit.Test)

Example 4 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtilsFiltersTest method testNonExistingField.

@Test
public void testNonExistingField() {
    QueryFilter qf = new QueryFilter();
    qf.setField("asdas");
    qf.setOperation(FilterOperation.EQ);
    qf.setValue("test");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    try {
        JPAUtils.queryEntities(em, User.class, q);
        Assert.fail("No exception was thrown");
    } catch (NoSuchEntityFieldException e) {
        Assert.assertEquals("asdas", e.getField());
    }
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) Test(org.junit.Test)

Example 5 with NoSuchEntityFieldException

use of com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException in project lynx by TFaga.

the class JPAUtilsFiltersTest method testNullField.

@Test
public void testNullField() {
    QueryFilter qf = new QueryFilter();
    qf.setOperation(FilterOperation.NEQ);
    qf.setValue("test");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    try {
        JPAUtils.queryEntities(em, User.class, q);
        Assert.fail("No exception was thrown");
    } catch (NoSuchEntityFieldException e) {
        Assert.assertEquals(null, e.getField());
    }
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) Test(org.junit.Test)

Aggregations

NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)7 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)6 QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)4 Test (org.junit.Test)4 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)3 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tuple (javax.persistence.Tuple)3 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)2 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)2 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)2 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)2 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)2 CriteriaFilter (com.github.tfaga.lynx.interfaces.CriteriaFilter)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 java.util (java.util)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 EntityManager (javax.persistence.EntityManager)2