简体   繁体   中英

how to Distinct in JPA Spring boot?

how to using distinct in JPA? because I want to get all phone numbers and name in my table, so i get all result by unique , my table name on a database called whatsapp_chat and I want to get by wa_id(my field database)

i tried Lits findDistincByWhatsappid() , Lits findDistincWhatsappid(); I got the error to use them, how to correct way to use Distinct? I am new in Spring boot and Java

i tried this also :

@Query("SELECT DISTINCT wa_id FROM whatsapp_chat")
    List<String> findDistinctWhatsappid();

hee is my entity/model :

Entity
@Data
@Table(name = "WHATSAPP_CHAT")
@DynamicUpdate
public class WhatsappChat extends Base {
    @Column(name = "NAME")
    private String name;

    @Column(name = "WA_ID")
    private String whatsappid;

    @Column(name = "TEXT")
    private String text;

    @Column(name = "MESSAGE_ID")
    private String messageid;

    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;

}

This is a HQL query, not a native one, hence it uses the class name of the entity along with its fields names.

Consider rewording your query like :

"SELECT DISTINCT whatsappid FROM WhatsappChat"

You have two options:

  1. Write native query:

     @Query(value = "SELECT DISTINCT w.WA_ID FROM WHATSAPP_CHAT w", nativeQuery = true) List<String> findDistinctWhatsappIds(); 
  2. Write JPQL query (querying using objects):

     @Query("SELECT DISTINCT w.whatsappid FROM WhatsappChat AS w") List<String> findDistinctWhatsappIds(); 

Either way, I would change the method name to be in plural (it is a list) and Uppercase in the Id (column retrieving) to know exactly what it is coming.

NOTE: The method name has no impact in the query itself as it contains the annotation @Query specifying what to execute.

You can test with this ( a little different if not using Spring-boot):

@DataJpaTest
@RunWith(SpringRunner.class)
public class WhatsappChatRepositoryTest {

    @Autowired
    EntityManager entityManager;

    @Autowired
    WhatsappChatRepository whatsappChatRepository;

    @Before
    public void setup() {
        entityManager.persist(new WhatsappChat("whats1", "whats01", 1));
        entityManager.persist(new WhatsappChat("whats2", "whats02", 2));
        entityManager.persist(new WhatsappChat("whats3", "whats01", 3));
        entityManager.persist(new WhatsappChat("whats4", "whats04", 4));

        entityManager.flush();
    }

    @Test
    public void shouldTestSameResult() {
        assertEquals(whatsappChatRepository.findDistinctWhatsappIds(), whatsappChatRepository.findDistinctWhatsappIdsNative());

        assertEquals(Arrays.asList("whats01", "whats02", "whats04"), whatsappChatRepository.findDistinctWhatsappIds());
    }
}

Do as Follows :

@Query(value="SELECT DISTINCT WA_ID FROM WHATSAPP_CHAT", nativeQuery = true)
    List<String> findDistinctWhatsappid();

You can use this instead of the one you are using, you can't use the column directly in the query at jpa until you set nativeQuery field to true. Use this

@Query(value = "SELECT DISTINCT W.whatsappid FROM WhatsappChat W")
List<String> findDistinctWhatsappid();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM