带有枚举的QuerySyntaxException [英] QuerySyntaxException with enum

查看:50
本文介绍了带有枚举的QuerySyntaxException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的UserAssignmentRole类:

I have a UserAssignmentRole class like this :

@Data
@Entity
public class UserAssignmentRole {
    ...
    @Enumerated(EnumType.STRING)
    public Role role;
}

角色是枚举,看起来像这样:

And the Role is enum, it looks like this:

public enum Role{
    admin,
    member,
    pending
}

现在,当我在存储库中尝试查询所有具有admin角色的人时,它给了我错误:

Now when in my repository I try to query to select all with role of admin, it gives me error:

@Query("select uar from UserAssignmentRole uar where uar.role=Role.admin")
public List<UserAssignmentRole> listAdmin(Long userID, Long assignmentID);

该如何解决?

错误: org.hibernate.hql.internal.ast.QuerySyntaxException:无效路径:"Role.admin"

完全错误: https://pastebin.com/tk9r3wDg

推荐答案

您可以尝试不自己编写该sql,而是使用类似以下代码的存储库创建代码:

You can try not to write this sql by yourself but with repository create code like this:

@Repository
public interface UserAssignmentRolelRepository extends JpaRepository<UserModel, Long>{

    public List<UserAssignmentRole> findByRole(Role role);

}

然后:

@Autowired
UserAssignmentRolelRepository repository ;

public void someMethod(){
   List<UserAssignmentRole> userAssignmentRoles = repository.findByRole(Role.admin);
}

更新1

在此答案中指出了 :非常规命名.您可以将枚举中的标签更改为大写.

UPDATE 1

As it was point out in this answer: non-conventional naming. You can change labels in your enum to uppercase.

public enum Role{
    Admin,
    Member,
    Pending
}

然后:

@Query("select uar from UserAssignmentRole uar where uar.role=com.example.package.Role.Admin")
public List<UserAssignmentRole> listAdmin(Long userID, Long assignmentID);

更新2

但是如果您真的想在数据库中使用小写字母.它需要更多代码才能更改.枚举更改为:

UPDATE 2

But if you really want to have lowercase in DB. It requires more code to change. Enum change to:

public enum Role{
    Admin("admin"),
    Member("member"),
    Pending("pending");

    private String name;

    Role(String name) {
        this.name = name;
    }

    public String getName() { return name; }

    public static Role parse(String id) {
        Role role = null; // Default
        for (Role item : Role.values()) {
            if (item.name.equals(id)) {
                role = item;
                break;
            }
        }
        return role;
    }
}

UserAssignmentRole

//    @Enumerated(EnumType.STRING)
    @Convert(converter = RoleConverter.class)
    private Role role;

以及其他班级:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class RoleConverter implements AttributeConverter<Role, String> {

    @Override
    public String convertToDatabaseColumn(Role role) {
        return role.getName();
    }

    @Override
    public Role convertToEntityAttribute(String dbData) {
        return Role.parse(dbData);
    }
}

这篇关于带有枚举的QuerySyntaxException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆