1

I am trying to implement Delete query in Spring Boot, however the parameters are optional. How do I write JPA query for same. Here is how I have implemented for mandate Request Params:

@Transactional
@Repository
public interface ABCRepo extends CrudRepository<ABC, Long>{

public List<ABC> findByABCIdAndStartYrAndStartMonth(String pilotId, int startYr, int startMonth);
public long deleteABCByABCId(String pilotId);
}

Controller.class

@RequestMapping(value="", method= RequestMethod.DELETE)
public Response delete(@PathVariable("abc-id")String pilotId)
{
    LOGGER.info("Trying to delete pilot bank using abc id : "+ abcId);
    long deletedRecords=abcBiz.deleteABCByABCId(abcId);
     if(deletedRecords==0)
     {
        throw new PilotNotFoundException("Entity not found "+abcId);
     }
    return Response.status(Response.Status.NO_CONTENT).entity(deletedRecords).build();
}

My new Controller.class after adding optional params

@RequestMapping(value="", method= RequestMethod.DELETE)
public Response delete(@PathVariable("abc-id")String abcId, @RequestParam(name = "bid-yr", required = false)
        int bidYr, @RequestParam(name = "bid-month", required = false) int bidMonth)
{
    LOGGER.info("Trying to delete pilot bank using abc id : "+ abcId);
    long deletedRecords=abcBiz.deleteABCByABCId(a);bcId
     if(deletedRecords==0)
     {
        throw new PilotNotFoundException("Entity not found "+abcId);
     }
    return Response.status(Response.Status.NO_CONTENT).entity(deletedRecords).build();
}

How do I handle this at JPA?

2 Answers 2

2

For optional parameters, you need to write the query. Something like below:

@Modifying
@Query("DELETE FROM ABC WHERE abcId=:pilotId AND (:otherOptionalParam IS NULL OR otherField=:otherOptionalParam)")
public long deleteABCByABCId(String pilotId, String otherOptionalParam);

If you want to create a complex query, with lot of optional parameters, then you can create custom repository, and develop native queries. Here I have already answered to how we can create custom repositories in Spring data JPA - https://stackoverflow.com/a/68721142/3709922

Sign up to request clarification or add additional context in comments.

6 Comments

Debug and check all the parameter values which are being passed to this repository method. There seems no issue with the query which you have stated in above comment.
here are the values: ("5422", null, null, null), these values are coming to repository method
But how come I know that these values exists in your database or not. Check with these values in your database.
5422 does exist but there is no column with bidyear or bidabbrev and bidmonth with null matching with 5422. My question is if incase of null, how can i ignore the fields and only fields which are having value.
By the above query they are ignored by default. (:bidYr IS NULL OR startYr=:bidYr) this will ignore the startYr comparison if bidYr parameter is null.
|
1

On Top of what Jignesh has said, don't forget to mark your parameters with Param annotation. Also jpa modification will return int/Integer but not long so I had to change return type too.

@Modifying
@Query("DELETE FROM ABC WHERE abcId=:pilotId AND (:otherOptionalParam IS NULL OR 
otherField=:otherOptionalParam)")
public long deleteABCByABCId(@Param("pilotId")String pilotId, @Param("otherOptionalParam")String 
otherOptionalParam);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.