0

I am trying to implement search functionality on grid using And condition. If am giving two parameter its giving Result. But if I want to search using only first field and second is null. Its giving empty array according to AND functionality. How to avoid null parameter in this situation.

If one parameter is null then how to avoid that parameter and get result according to that parameter which is not null.

AccountController

@RestController
@RequestMapping("/SpaceStudy/SpaceAdmin")
public class AccountController {

    @Autowired
    AccountService accService;

    @CrossOrigin(origins = "*")
    @GetMapping("/AccountMaintenance/LoadGrid")
    public Set<AccountModel> GridLoad( String sclientacctid, String sacctdesc) {
        return accService.gridLoad("1124100",null);

AccountService

@Service
public class AccountService {
    @Autowired
    AccountRepository accRepo;
public Set<AccountModel> gridLoad( String sclientacctid,String sacctdesc) {

        if (sclientacctid != null || sacctdesc != null) {
            Set<AccountModel> gridObj1 = accRepo.findBySclientacctidAndSacctdesc(sclientacctid, sacctdesc);
            return gridObj1;
        }       
            Set<AccountModel> gridObj = accRepo.findBySclientacctid();

        return gridObj;
    }

AccountRepository

@Repository
public interface AccountRepository extends JpaRepository<AccountModel, Integer>,AccontRepositoryCustom {

    @Query("select distinct(a.slocation) from AccountModel a where a.slocation !=null")
    List<AccountModel> findBySlocation();   

    @Query("select new map(acct.sclientacctid as sclientacctid ,acct.sacctdesc as sacctdesc ,"
            + "acct.slocation as slocation,invest.sinvestigatorname as sinvestigatorname ,"
            + "dept.sclientdeptid as sclientdeptid,dept.sdeptname as sdeptname ,acp.sccpcode as sccpcode)"
            + " from AccountModel acct join acct.department dept"
            + " join acct.investigator invest join acct.accountCPC acp" + " where acct.ninstid= 60")
    Set<AccountModel> findBySclientacctid();

AccountRepositoryCustomImpl

@Repository
public class AccountRepositoryCustomImpl implements AccontRepositoryCustom {

    @PersistenceContext
    EntityManager entityManager;

    @SuppressWarnings("unchecked")
    @Override
    public Set<AccountModel> findBySclientacctidAndSacctdesc(String sclientacctid, String sacctdesc) {

        Query query = entityManager
                .createQuery("select new map(acct.sclientacctid as sclientacctid ,acct.sacctdesc as sacctdesc ,"
                        + "acct.slocation as slocation,invest.sinvestigatorname as sinvestigatorname ,"
                        + "dept.sclientdeptid as sclientdeptid,dept.sdeptname as sdeptname ,acp.sccpcode as sccpcode)"
                        + " from AccountModel acct join acct.department dept"
                        + " join acct.investigator invest join acct.accountCPC acp" 
                        + " where acct.sclientacctid=?1 and acct.acctdesc=?2 ");
        query.setParameter(1, sclientacctid);
        query.setParameter(2, sacctdesc);
        return (Set<AccountModel>) query.getResultList();
    }     
}

I am new in spring. Can any one help me,what I need to change in above code?

Ram
  • 1,743
  • 2
  • 18
  • 40
SpringUser
  • 1,351
  • 4
  • 29
  • 59

1 Answers1

0

Your may try something like below (not tested though):

    Query query = entityManager
            .createQuery("select new map(acct.sclientacctid as sclientacctid ,acct.sacctdesc as sacctdesc ,"
                    + "acct.slocation as slocation,invest.sinvestigatorname as sinvestigatorname ,"
                    + "dept.sclientdeptid as sclientdeptid,dept.sdeptname as sdeptname ,acp.sccpcode as sccpcode)"
                    + " from AccountModel acct join acct.department dept"
                    + " join acct.investigator invest join acct.accountCPC acp" 
                    + " where acct.sclientacctid=?1 and (?2 is null or acct.acctdesc =?2) ");
Ram
  • 1,743
  • 2
  • 18
  • 40