简体   繁体   English

如何在JPA Spring启动中与众不同?

[英]how to Distinct in JPA Spring boot?

how to using distinct in JPA? 在JPA中如何使用distinct? 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) 因为我想获取表中的所有电话号码和姓名,所以我通过unique来获取所有结果,我的表​​名在名为whatsapp_chat的数据库中,并且我想通过wa_id(我的字段数据库)获取

i tried Lits findDistincByWhatsappid() , Lits findDistincWhatsappid(); 我试过Lits findDistincByWhatsappid(),Lits findDistincWhatsappid(); I got the error to use them, how to correct way to use Distinct? 使用它们时出现错误,如何正确使用Distinct? I am new in Spring boot and Java 我是Spring Boot和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. 这是一个HQL查询,而不是本机查询,因此它使用实体的类名及其字段名。

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): 编写JPQL查询(使用对象查询):

     @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. 无论哪种方式,我都将方法名称更改为复数形式(它是一个列表),并在Id中将其大写形式(列检索)更改为确切知道它即将来临。

NOTE: The method name has no impact in the query itself as it contains the annotation @Query specifying what to execute. 注意:方法名称对查询本身没有影响,因为它包含注释@Query指定要执行的内容。

You can test with this ( a little different if not using Spring-boot): 您可以对此进行测试(如果不使用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. 您可以使用此列而不是正在使用的列,除非将nativeQuery字段设置为true,否则无法直接在jpa的查询中使用该列。 Use this 用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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