What kind of return of a select Count(*) in Spring JPA?

Asked

Viewed 4,853 times

4

I need to know the type of return that Spring JPA returns to put in the Service package, because 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();
    }
}
  • What is the complete error? At what point does Nullpointerexception?

4 answers

2

The interface JpaRepository (who inherits from CrudRepository) already comes with a ready method for this action.
He calls himself count() and returns a long.

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

See the documentation for more information.

1

He returns a Long due to his Count nature.

TypedQuery<Long>
  • I tested with int, Integer, long, Long and always from Nullpointerexception

  • Does the query return anything? I mean, if you have tested it yourself in the database without java

  • in the database the query is different, it is query SQL ("select Count() from playlists") returns a Count column() with the amount of records

  • 1

    Edit your question and ask stacktrace of Exception that you are receiving. Take advantage of and delete the other issue that you opened regarding this code.

  • Try to take the SQL query referring to your JPA, and test that same query directly in the database to see if it returns something

  • The fact of returning NULL is most likely not related to the type of return data.

Show 1 more comment

1

You can use the Spring JPA Repository notation to do the count, as igventurelli said:

public interface PlaylistDao extends JpaRepository<Playlist, Long> {

    long count();

}

If you want to count by some attribute of the entity, just add the

long countByName();

If you wish to make a count more complex, involving the @Query, you can do:

@Query("SELECT count(*) FROM Playlist")
long countPlaylist();

If you want to use the Entity Manager with the following Jpql:

String jpql = "SELECT COUNT(*) FROM Playlist");

You can use the TypedQuery<Long>:

TypedQuery<Long> query = entityManager.createQuery(jpql , Long.class);
long total = query.getSingleResult();

Or Number:

Query queryCount = entityManager.createQuery(jpql);
long total = ((Number) queryCount.getSingleResult()).longValue();

To read the final result.

0

Good practice of using a jpql and putting an alias

@Query("select count(p) from Playlist")
public Long getSizeOfPlaylist();

However strange the Crudrepository’s Count is not working as it is native to spring

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

since this excerpt above and well redundant, well I would say that all this code to catch only the quantity has no need, you do this with 1 line;

dao.findAll().size();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.