Search code examples
javamysqlspring-bootspring-data-jpajpql

Multiple clause query in spring boot


package com.mobile.model;

import javax.persistence.*;


@Entity
@Table(name = "mobile_table")
public class Mobile {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public int mobileId;

    @Column(name = "mobile_name",nullable = false,length = 20)
    public String mobileName;

    @Column(name = "mobile_brand",nullable = false,length = 15)
    public String mobileBrand;

    @Column(name="mobile_networkType")
    public String mobileNetworkType;

    @Column(name = "mobile_core")
    public Integer mobileAvailableCore;

    @Column(name = "mobile_ram")
    public Integer mobileRAMSize;

    @Column(name = "mobile_os")
    public String mobileOS;

    @Column(name = "mobile_wifif")
    public Boolean mobileWifiAvailability;

    @Column(name = "mobile_bluetooth")
    public Boolean mobileBluethoothAvailablity;

    public Mobile() {
    }

    public Mobile(String mobileName, String mobileBrand, String mobileNetworkType, Integer mobileAvailableCore, Integer mobileRAMSize, String mobileOS, Boolean mobileWifiAvailability, Boolean mobileBluethoothAvailablity) {
        this.mobileName = mobileName;
        this.mobileBrand = mobileBrand;
        this.mobileNetworkType = mobileNetworkType;
        this.mobileAvailableCore = mobileAvailableCore;
        this.mobileRAMSize = mobileRAMSize;
        this.mobileOS = mobileOS;
        this.mobileWifiAvailability = mobileWifiAvailability;
        this.mobileBluethoothAvailablity = mobileBluethoothAvailablity;
    }
}

//Query for multiple selection
    @SuppressWarnings("unchecked")
	public List<Mobile> getMobileSearch(String mobileBrand, Integer mobileRAMSize, String mobileOS){
    	return mobileDao.findAll(Specifications.where((Specification<Mobile>) getAllMobilesByBrand(mobileBrand))
    			.and((Specification<Mobile>) getAllMobilesByOS(mobileOS))
    			.and((Specification<Mobile>) getAllMobilesByRam(mobileRAMSize)));
    }

// calling function for multiple attribute Search in controller class
@RequestMapping(value="/searchMobile", method=RequestMethod.GET)
    public ResponseEntity<List<Mobile>> searchMobile(@RequestParam(value="brand") String mobileBrand, @RequestParam(value="ramSize") Integer ramSize, @RequestParam(value="mobileOs") String MobileOs)
    {
    	List<Mobile> mobileList=mobileService.getMobileSearch(mobileBrand,ramSize,MobileOs);
    	return new ResponseEntity<List<Mobile>>(mobileList,HttpStatus.OK);
    }

// here is how my database is calling database query.
@Repository
public interface MobileDao extends CrudRepository<Mobile, Integer>, JpaSpecificationExecutor<Mobile> {

    public List<Mobile> findByMobileBrand(String mobileBrand);

    @Query("select distinct m.mobileBrand from Mobile m")
    public List<String > getAllDistinctBrand();

    public List<Mobile> findByMobileRAMSize(Integer mobileRAMSize);

    @Query("select distinct m.mobileRAMSize from Mobile m")
    public List<Integer> getAllDistinctRam();

    public List<Mobile> findByMobileOS(String mobileOS);


    @Query("select distinct m.mobileOS from Mobile m")
    public List<String > getAllDistinctMobileOS();

I am trying to implement the search query for products like used in different e-commerce Websites(like Flipkart, Amazon) where user inserts different attributes for the product. Based on the product Attribute, results are shown. For the implementation i am using Spring Boot and JPA as a database.

How can i search multiple clause in single query dynamically. For Example, in case of SQL query we search multiple clause like this.

How can i implement multiple clause in JPA.

Select * from table_name where attribute1="Attb" and attribute2="Attb2" and attribute3="Attb3";

Attb, Attb2 and Attb3 are dynamically changing based on client input.


Solution

  • Your question was not clear enought. If you need to use variable number of conditions, you will have to use Criteria API. It allows you to conditionally create list of predicates and use that for data fetch. http://www.objectdb.com/java/jpa/query/criteria

    It will look like this:

        myQueryMethod(UserChoices choices){
            if(choices.someChoice1){
             predicates.add(somePredicate)
            }
        .....