(playframework 2.0.2-java)EBean-使用枚举值查询时,没有ScalarType注册错误 [英] (playframework 2.0.2-java) EBean - No ScalarType registered error when querying with enum values
问题描述
我有Role实体类:
@Entity
public class Role extends Model {
@Id
@Constraints.Required
public Integer id;
@Constraints.Required
@Formats.NonEmpty
@Enumerated(EnumType.STRING)
public RoleNameEnum name; // name is enum value
}
在某些测试中,我尝试按角色查找用户:
In some test I try to find users by role:
List<User> users = User.findByRole(Role.findByRoleName(RoleNameEnum.ADMIN));
方法findByRoleName()
在以下位置:
public static List<User> findByRole(Role role) {
return find.where().eq("role", role).findList();
}
我收到错误消息:
[error] Test UserTest.findUsersByRole failed: No ScalarType registered for class models.Role
[error] at com.avaje.ebeaninternal.server.persist.Binder.bindObject(Binder.java:183)
[error] at com.avaje.ebeaninternal.server.query.CQueryPredicates.bind(CQueryPredicates.java:162)
[error] at com.avaje.ebeaninternal.server.query.CQuery.prepareBindExecuteQuery(CQuery.java:413)
[error] at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:198)
[error] at com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:104)
[error] at com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:344)
[error] at com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1469)
[error] at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findList(DefaultOrmQuery.java:906)
[error] at com.avaje.ebeaninternal.util.DefaultExpressionList.findList(DefaultExpressionList.java:201)
[error] at models.User.findByRole(User.java:63)
[error] at UserTest$4.run(UserTest.java:62)
[error] at play.test.Helpers.running(Helpers.java:294)
[error] at UserTest.findUsersByRole(UserTest.java:58)
有人知道什么可能是个问题吗?
Does anybody have an idea what might be a problem?
推荐答案
最快的解决方案,假设您要映射的EnumValue与枚举名称完全相同:
The quickest solution, assuming that you are mapping EnumValue exactly the same as the enum names:
public enum RoleNameEnum {
@EnumValue("REGULAR")
REGULAR,
@EnumValue("ADMIN")
ADMIN
}
然后,您可以实现findByRole方法,如下所示:
Then you can implement the findByRole method as following:
public static List<User> findByRole(Role role) {
return find.where().eq("role", role.name()).findList();
}
魔术只使用映射的字符串值而不是角色名称的枚举实例.
where the magic is just using the mapped string value instead of the enum instance for the role name.
我在ebean问题跟踪器上发布了一个错误: http://www.avaje.org/bugdetail-427.html ,活页夹应检测到枚举对象并将其自动解释为其映射值.
I posted a bug on the ebean issue tracker: http://www.avaje.org/bugdetail-427.html, binder should detect the enum object and interpret it as its mapped value automatically.
如果您需要除简单枚举值之外的其他映射,这是使用@EnumValue批注获取值集的实用程序代码
In case that you need some other mapping than the simple enum value, here it is the utility code to get the value set using the @EnumValue annotation
public static <T extends Enum<T>> String serialize(T theEnum) {
try {
for (Field f : theEnum.getDeclaringClass().getDeclaredFields()) {
if (theEnum.equals(f.get(theEnum))) {
EnumValue enumValue = f.getAnnotation(EnumValue.class);
if (enumValue != null)
return enumValue.value();
}
}
} catch (Exception e) {
}
return null;
}
然后您可以使用序列化方法实现findByRole
Then you can implement findByRole using the serialize method
public static List<User> findByRole(Role role) {
return find.where().eq("role", serialize(role)).findList();
}
这篇关于(playframework 2.0.2-java)EBean-使用枚举值查询时,没有ScalarType注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!