Search code examples
javaspring-bootjpaspring-data-jparepository

Spring JPA query for checking boolean property


Suppose i have the following entity:

@Entity
public class MyClass {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Boolean isActive;
}

I want to check if an object "MyClass" isActive this is what i have done:

public interface MyClassRepository extends Repository<MyClass, Long> {
    @Query("SELECT IS_ACTIVE FROM T_MYTABLE WHERE ID = ?1")
    public Boolean isActive(Long id);
}

However i get the following error

org.springframework.orm.jpa.JpaSystemException: Specified result type [java.lang.Boolean] did not match Query selection type [com.myapp.MyClass] - multiple selections: use Tuple or array

What is the correct why to implement this?


Solution

  • You are mixing jpa query with native query. In your case you should:

    Native query:

    @Query(value = "SELECT IS_ACTIVE FROM T_MYTABLE WHERE ID = ?1", nativeQuery = true)
    

    JPQL:

    @Query("SELECT isActive FROM MyClass WHERE id= ?1")