java – What return type of a select count(*) in Spring JPA?

Question:

I need to know the type of return that Spring JPA returns to put in the Service package, as it is giving a NullPointerException :

Dao:

public interface PlaylistDao extends JpaRepository<Playlist, Long> {
    @Query("select count(*) from Playlist")
    public int verifica();
}

Service:

@Service
@Transactional
public class PlaylistServiceImpl implements PlaylistService {

    @Autowired
    private PlaylistDao dao;

    public int verifica(){
        return dao.verifica();
    }
}

Answer:

The JpaRepository interface (which inherits from CrudRepository ) already comes with a ready-made method for this action.
It's called count() and returns a long .

Instead of declaring a new method, you can use this one.

See the documentation for more information.

Scroll to Top