Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data: Enumeration and Repository issue

I am using Spring Data and Repositories. I created an Entity with a enum type field, which I declared @Enumerated(EnumType.STRING), but I am forced to create a method getAuthority returning a String.

@Entity
@Configurable
public class BaseAuthority implements GrantedAuthority {

    @Enumerated(EnumType.STRING)
    @Column(unique = true)
    private AuthorityType authority;

    @Override
    public String getAuthority() {
        return authority.toString();
    }

}

The enum is as follow:

public enum AuthorityType {
REGISTEREDUSER, ADMINISTRATOR;
}

In the repository for the entity, I created an operation to find by authority type:

@Repository
public interface BaseAuthorityRepository extends JpaRepository<BaseAuthority, Long> {

    BaseAuthority findByAuthority(AuthorityType authority);

}

However, I get a warning:

Parameter type (AuthorityType) does not match domain class 
property definition (String). BaseAuthorityRepository.java

I used to have the operation receiving a String rather than AuthorityType, but that generates a runtime exception. I could change the name of the field authority to authorityType, but I don't like that.

Am I doing something wrong? How can I remove the warning?

like image 690
Manu Avatar asked Aug 06 '14 13:08

Manu


People also ask

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is difference between JpaRepository and repository?

JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.

What is difference between CrudRepository and JpaRepository interfaces in Spring data JPA?

Each of these defines its own functionality: CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Why do we use JpaRepository?

JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.


1 Answers

I guess you have to rename the field, but you can do it in an transparent way:

@Entity
public class BaseAuthority implements GrantedAuthority {
    private static final long serialVersionUID = 1L;

    @Enumerated(EnumType.STRING)
    @Column(unique = true, name = "authority")
    private AuthorityType authorityType;

    AuthorityType getAuthorityType() {
        return authorityType;
    }

    @Override
    public String getAuthority() {
        return authorityType.toString();
    }
}

and change your repository to

@Repository
public interface BaseAuthorityRepository extends JpaRepository<BaseAuthority, Long> {

    BaseAuthority findByAuthorityType(AuthorityType authority); 
}
like image 134
Christof R Avatar answered Oct 11 '22 18:10

Christof R