You can execute your first query by calling Query.getSingleResult()
, eg.
String queryString = "SELECT Count(*) FROM my_table";
Query query = entityManager.createNativeQuery(queryString);
System.out.println(query.getSingleResult());
If you want to assign the count to a variable you need to cast it to proper type (it can depend on DB but most likely it's Long).
The second query is very inefficient because Hibernate needs to retrieve entire table from DB, analyze it and then count all rows.