title: How to count row table in JPA Query
body: <p>I'm new to Spring Boot. I have a mysql database, I use a query to count row in my table. But it's not work, it still return my original table data. Can you help me check my code.</p>
  
  <p>Here is my Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "act_id_membership", schema = "activiti", catalog = "")
  @IdClass(ActIdMembershipEntityPK.class)
  public class ActIdMembershipEntity {
      private String userId;
      private String groupId;
  
      @Id
      @Column(name = "USER_ID_")
      public String getUserId() {
          return userId;
      }
  
      public void setUserId(String userId) {
          this.userId = userId;
      }
  
      @Id
      @Column(name = "GROUP_ID_")
      public String getGroupId() {
          return groupId;
      }
  
      public void setGroupId(String groupId) {
          this.groupId = groupId;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
          ActIdMembershipEntity that = (ActIdMembershipEntity) o;
          return Objects.equals(userId, that.userId) &amp;&amp;
                  Objects.equals(groupId, that.groupId);
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(userId, groupId);
      }
  }
  </code></pre>
  
  <p>Here is my query:</p>
  
  <pre><code>@Repository
  public interface MemershipRepository extends JpaRepository&lt;ActIdMembershipEntity, String&gt; {
      @Query ("select new com.example.activiti_restful.dtos.UserMembership(i.userId, count(i)) from ActIdMembershipEntity i where i.userId ='kermit'")
      UserMembership countMemberships(String userId);
  }
  </code></pre>
  
  <p><strong>Updated code:</strong>
  My service class:</p>
  
  <pre><code>@Service
  public class MembershipService {
  @Autowired
  private MemershipRepository repository;
  
  public long count() {
      return repository.count();
  }
  </code></pre>
  
  <p>My resource class:</p>
  
  <pre><code>@RestController
  public class MembershipResource {
  @Autowired
  private MembershipService membershipService;
  
  @GetMapping("/membership")
  public long list() {return membershipService.count();}
  }
  </code></pre>
  
  <p>My custom JSON Object class:</p>
  
  <pre><code>public class UserMembership {
      private String userId;
  
      private long numberOfusers;
  
      public UserMembership(String userId, long numberOfusers) {
          this.userId = userId;
          this.numberOfusers = numberOfusers;
      }
  } 
  </code></pre>
  
  <p>MySQL Table:
  <a href="https://i.stack.imgur.com/zsdnn.png" rel="noreferrer">act_id_membership</a></p>