0

I do not understand how queries work in spring. So far, I have this query where I want to retrieve a value from a table based on string arguments. But from my research, in order to implement the query annotation, I am supposed to put the query annotation above a function. But how do i retrieve the value from this subroutine when it hasn't been implemented yet?

I start off from here

@Repository
public interface PhoneRepository {



@Query("SELECT price FROM phone WHERE model = ?1 AND storage = ?2 AND quality = ?3")
public double devicePrice(String model, String storage, String quality);

} 

How am I supposed to get the price value from the table from here?

public class PhoneImplementation implements PhoneRepository{


    public double devicePrice(String model, String storage, String quality) {

        // return the price value here somehow
    }



}
Tony
  • 83
  • 1
  • 9
  • are you looking for custom implementation of `devicePrice` method? – Ryuzaki L Mar 03 '19 at 02:03
  • @Deadpool I am not looking for a custom implementation. I am just looking for a query that returns a value from my table. I have no understanding how this works, and i tried to read the spring.io documentation with no avail. Thank you. – Tony Mar 03 '19 at 02:04
  • what's wrong with query? it does not execute? or any error? or it is not returning anything ? – Ryuzaki L Mar 03 '19 at 02:07
  • @Deadpool I haven't tried to do anything with query to be honest. I actually do not know how to set this up where I just call the function and it retrieves my value. Essentially, i just want to do this: `double returnedValue = devicePrice(String x, String y, String z);` And then this will return a value. – Tony Mar 03 '19 at 02:11

2 Answers2

0

You do not need to implement the @Repository PhoneRepository. Just call the PhoneRepository on your Controller or Service with @Autowired and use devicePrice (String model, String storage, String quality).

Example in our Service:

@Autowired
private PhoneRepository phoneRepository;

public double yourMethod(String model, String storage, String quality) {
    return this.phoneRepository.devicePrice(String model, String storage, String quality);
}
Neto Deolino
  • 79
  • 1
  • 5
0
@Transactional
@Query(value="SELECT price FROM phone WHERE model = ?1 AND storage = ?2 AND quality = ?3",nativeQuery=true)
public double devicePrice(String model, String storage, String quality);

As you are going to use Spring Boot you can extend your repository with JpaRepository<T, ID> as below

public interface PhoneRepository extends JpaRepository<Phone, ID>

Here Phone is your entity and ID is a type of your primary key in your entity. Jpa makes easy to write the custom query without writing in your repository.


JpaRepository already have the following methods

    List<T> findAll();
    List<T> findAll(Sort sort);
    List<T> findAllById(Iterable<ID> ids);
    <S extends T> List<S> saveAll(Iterable<S> entities);
    <S extends T> S saveAndFlush(S entity);
    void deleteAllInBatch();
    <S extends T> List<S> findAll(Example<S> example);

Even you can write your queries based on reference fields.

.findByLastnameAndFirstname (… where x.lastname = ?1 and x.firstname = ?2)

You shold refer the documents Doc 1 Que 1

Romil Patel
  • 12,879
  • 7
  • 47
  • 76