JPA: How to get entity based on field value other than ID?

前端 未结 15 957
时光说笑
时光说笑 2020-12-04 21:08

In JPA (Hibernate), when we automatically generate the ID field, it is assumed that the user has no knowledge about this key. So, when obtaining the entity, user would query

15条回答
  •  被撕碎了的回忆
    2020-12-04 21:47

    if you have repository for entity Foo and need to select all entries with exact string value boo (also works for other primitive types or entity types). Put this into your repository interface:

    List findByBoo(String boo);
    

    if you need to order results:

    List findByBooOrderById(String boo);
    

    See more at reference.

提交回复
热议问题