Spring data JPA query with parameter properties

后端 未结 9 1142
既然无缘
既然无缘 2020-11-29 00:20

What is the simplest way of declaring a Spring data JPA query that uses properties of an input parameter as query parameters?

For example, suppose I have an entity c

相关标签:
9条回答
  • 2020-11-29 01:02
        for using this, you can create a Repository for example this one:
        Member findByEmail(String email);
    
        List<Member> findByDate(Date date);
        // custom query example and return a member
       @Query("select m from Member m where m.username = :username and m.password=:password")
            Member findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
    
    0 讨论(0)
  • 2020-11-29 01:04

    You can try something like this:

    public interface PersonRepository extends CrudRepository<Person, Long> {
           @Query("select p from Person AS p"
           + " ,Name AS n"  
           + " where p.forename = n.forename "
           + " and p.surname = n.surname"
           + " and n = :name")
           Set<Person>findByName(@Param("name") Name name);
        }
    
    0 讨论(0)
  • 2020-11-29 01:07

    What you want is not possible. You have to create two parameters, and bind them separately:

    select p from Person p where p.forename = :forename and p.surname = :surname
    ...
    query.setParameter("forename", name.getForename());
    query.setParameter("surname", name.getSurname());
    
    0 讨论(0)
  • 2020-11-29 01:09

    This link will help you: Spring Data JPA M1 with SpEL expressions supported. The similar example would be:

    @Query("select u from User u where u.firstname = :#{#customer.firstname}")
    List<User> findUsersByCustomersFirstname(@Param("customer") Customer customer);
    

    https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions

    0 讨论(0)
  • 2020-11-29 01:09
    @Autowired
    private EntityManager entityManager;
    
    @RequestMapping("/authors/{fname}/{lname}")
        public List actionAutherMulti(@PathVariable("fname") String fname, @PathVariable("lname") String lname) {
            return entityManager.createQuery("select A from Auther A WHERE A.firstName = ?1 AND A.lastName=?2")
                    .setParameter(1, fname)
                    .setParameter(2, lname)
                    .getResultList();
        }
    
    0 讨论(0)
  • 2020-11-29 01:12

    if we are using JpaRepository then it will internally created the queries.

    Sample

    findByLastnameAndFirstname(String lastname,String firstname)

    findByLastnameOrFirstname(String lastname,String firstname)

    findByStartDateBetween(Date date1,Date2)

    findById(int id)

    Note

    if suppose we need complex queries then we need to write manual queries like

    @Query("SELECT salesOrder FROM SalesOrder salesOrder WHERE salesOrder.clientId=:clientId AND salesOrder.driver_username=:driver_username AND salesOrder.date>=:fdate AND salesOrder.date<=:tdate ")
     @Transactional(readOnly=true)
     List<SalesOrder> findAllSalesByDriver(@Param("clientId")Integer clientId, @Param("driver_username")String driver_username, @Param("fdate") Date fDate, @Param("tdate") Date tdate);
    
    0 讨论(0)
提交回复
热议问题