Search code examples
spring-data-jpaspring-dataeclipselinkjpql

Exception when using @Query to search for a UUID derived from an abstract class


I have an abstract class with:

@Column(name = "uuid", nullable = false, unique = true, updatable = false, length = 36)
    private String uuid;

    public UUID getUuid() {
            if (uuid == null) {
                return null;
            }
        return UUID.fromString(uuid);
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid.toString();
    }

and then an entity:

@Entity
@Table(name = "creatinine")
public class Creatinine extends AbstractEntity {

and repository

public interface CreatinineRepository extends JpaRepository<Creatinine, Long> {
    @Query("SELECT m FROM Creatinine m WHERE m.uuid = :uuid")
    public Creatinine find(String uuid);
}

However the application is throwing an "you have attempted to set a parameter that does not exist in this query string" exception.

I have passed the uuid in the query both as a String and a UUID with the same exception in both cases. Is there something that I am missing here?


Solution

  • I think when using named parameters need to add @Param

    public interface CreatinineRepository extends JpaRepository<Creatinine, Long> {
        @Query("SELECT m FROM Creatinine m WHERE m.uuid = :uuid")
        public Creatinine find(@Param("uuid") String uuid);
    }
    

    If you are using Java 8 or better be sure to compile with -parameters to avoid @Param annotation for named parameters.