mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-04 04:07:42 +08:00
PL-6500 Implicit conversion for query parameters of Enum type
This commit is contained in:
parent
7d9b02c81a
commit
d79c1c0367
@ -17,6 +17,7 @@
|
||||
package com.haulmont.cuba.core.sys;
|
||||
|
||||
import com.haulmont.bali.util.ReflectionHelper;
|
||||
import com.haulmont.chile.core.datatypes.impl.EnumClass;
|
||||
import com.haulmont.chile.core.model.MetaClass;
|
||||
import com.haulmont.cuba.core.TypedQuery;
|
||||
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);
|
||||
}
|
||||
value = list;
|
||||
} else if (value instanceof EnumClass) {
|
||||
value = ((EnumClass) value).getId();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
@ -18,7 +18,10 @@ package com.haulmont.cuba.core;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
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.Role;
|
||||
import com.haulmont.cuba.security.entity.RoleType;
|
||||
import com.haulmont.cuba.security.entity.User;
|
||||
import com.haulmont.cuba.testsupport.TestContainer;
|
||||
import org.junit.After;
|
||||
@ -439,4 +442,44 @@ public class QueryTest {
|
||||
assertNotNull(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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user