PL-6500 Implicit conversion for query parameters of Enum type

This commit is contained in:
Konstantin Krivopustov 2016-05-31 13:00:17 +04:00
parent 7d9b02c81a
commit d79c1c0367
2 changed files with 46 additions and 0 deletions

View File

@ -17,6 +17,7 @@
package com.haulmont.cuba.core.sys; package com.haulmont.cuba.core.sys;
import com.haulmont.bali.util.ReflectionHelper; import com.haulmont.bali.util.ReflectionHelper;
import com.haulmont.chile.core.datatypes.impl.EnumClass;
import com.haulmont.chile.core.model.MetaClass; import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.TypedQuery; import com.haulmont.cuba.core.TypedQuery;
import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.entity.Entity;
@ -360,6 +361,8 @@ public class QueryImpl<T> implements TypedQuery<T> {
list.add(obj instanceof Entity ? ((Entity) obj).getId() : obj); list.add(obj instanceof Entity ? ((Entity) obj).getId() : obj);
} }
value = list; value = list;
} else if (value instanceof EnumClass) {
value = ((EnumClass) value).getId();
} }
return value; return value;
} }

View File

@ -18,7 +18,10 @@ package com.haulmont.cuba.core;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.haulmont.cuba.core.sys.QueryImpl;
import com.haulmont.cuba.security.entity.Group; import com.haulmont.cuba.security.entity.Group;
import com.haulmont.cuba.security.entity.Role;
import com.haulmont.cuba.security.entity.RoleType;
import com.haulmont.cuba.security.entity.User; import com.haulmont.cuba.security.entity.User;
import com.haulmont.cuba.testsupport.TestContainer; import com.haulmont.cuba.testsupport.TestContainer;
import org.junit.After; import org.junit.After;
@ -439,4 +442,44 @@ public class QueryTest {
assertNotNull(active); assertNotNull(active);
assertTrue(active); assertTrue(active);
} }
@Test
public void testEnumImplicitConversion() throws Exception {
// explicit enum id value
cont.persistence().runInTransaction(em -> {
TypedQuery<Role> query = em.createQuery("select r from sec$Role r where r.type = :roleType", Role.class);
query.setParameter("roleType", 10);
List<Role> roles = query.getResultList();
assertTrue(roles.stream().anyMatch(role -> role.getName().equals("Administrators")));
});
// enum as a positional parameter
cont.persistence().runInTransaction(em -> {
TypedQuery<Role> query = em.createQuery("select r from sec$Role r where r.type = ?1", Role.class);
query.setParameter(1, RoleType.SUPER);
List<Role> roles = query.getResultList();
assertTrue(roles.stream().anyMatch(role -> role.getName().equals("Administrators")));
});
// enum as a named parameter
cont.persistence().runInTransaction(em -> {
TypedQuery<Role> query = em.createQuery("select r from sec$Role r where r.type = :roleType", Role.class);
query.setParameter("roleType", RoleType.SUPER);
List<Role> roles = query.getResultList();
assertTrue(roles.stream().anyMatch(role -> role.getName().equals("Administrators")));
});
// no implicit conversions - fails
try (Transaction tx = cont.persistence().createTransaction()) {
TypedQuery<Role> query = cont.entityManager().createQuery("select r from sec$Role r where r.type = :roleType", Role.class);
query.setParameter("roleType", RoleType.SUPER, false);
List<Role> roles = null;
try {
roles = query.getResultList();
fail();
} catch (Exception e) {
// ok
}
}
}
} }