Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Is it possible to use enum parameter with @Query annotation?

Here is the code I'm using to find user role:

Role userRole = roleRepository.findByRole(Roles.USER);
if ( userRole == null ) {
    LOGGER.debug("No role found with role: {}", Roles.USER);
}

and it prints out

No role found with role: ROLE_USER

but if I try to find all roles this is what I get:

for ( Role r : roleRepository.findAll() )
    LOGGER.debug("{}", r);

Role@8a8c0a[roleId=1,role=role_admin,version=0]
Role@1efe9ee[roleId=2,role=role_staff,version=0]
Role@1e70f68[roleId=3,role=role_user,version=0]
Role@a475d1[roleId=4,role=role_guest,version=0]

As you can see user role does exists.

RoleRepository:

public interface RoleRepository extends JpaRepository<Role, Long> {

    @Query("SELECT r FROM Role r WHERE LOWER(r.role) = LOWER(:role)")
    public Role findByRole(@Param("role") Roles role);

}

Role:

@Entity
@Table(name = "role")
public class Role {

    public enum Roles {

        ADMIN("ROLE_ADMIN"),
        STAFF("ROLE_STAFF"),
        USER("ROLE_USER"),
        GUEST("ROLE_GUEST");

        private String role;

        private Roles(String role) {
            this.role = role;
        }

        public String getRole() {
            return role;
        }

        @Override
        public String toString() {
            return role;
        }

    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id", updatable = false)
    private Long roleId;

    @Column(name = "role")
    private String role;

    @Version
    @Column(name = "version")
    private long version = 0;

    public Long getRoleId() {
        return roleId;
    }

    public String getRole() {
        return role;
    }

    public long getVersion() {
        return version;
    }

    @Override
    public String toString() {
        return new ReflectionToStringBuilder(this).toString();
    }

}
share|improve this question

I suggest proper use of JPA enumerated types. Change the type "role" property as:

@Column(name = "role")
@Enumerated(EnumType.STRING)
private Roles role;

This should automatically fix the query result.

share|improve this answer
2  
Tried this without any success. After enabling more specific logging I got this message: 09:20:28.953 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARBINARY] - ROLE_USER. Does it bind parameter as wrong type? – perak Jun 22 '13 at 6:22
    
Maybe try to use a "String" param in the repository method, instead of the Enum directly. If you use native SQL in the query, try to use "like" – frandevel Oct 23 at 20:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.