1

I am using Spring Data JPA for creating services. I am trying to use IN clause in JPQL Query.

Actually I am trying to convert this LinQ query to JPQL

LinQ Query

from rooms in EspaceDB.Rooms
                            where roomIDList.Contains(rooms.nRoomID.ToString())
                            select rooms;

java.lang.NoSuchMethodException: userAuth.User.<init>()

This solution didn't work for me. In my all model classes I am having default constructor.

JPQL Query Syntax

@Query("select room from Room as room where room.nRoomId In (:nRoomIdList)")    
    List<Room> recoverDeletedRoom(@Param(value = "nRoomIdList") List<Integer> nRoomIdList);

Console

java.lang.NoSuchMethodException: java.util.List.<init>()
    at java.lang.Class.getConstructor0(Unknown Source) ~[na:1.8.0_144]
    at java.lang.Class.getDeclaredConstructor(Unknown Source) ~[na:1.8.0_144]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]

    at java.lang.Thread.run(Unknown Source) [na:1.8.0_144]

Room class

@Entity
@Table(name = "room")
public class Room implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "room_seq_generator")
    @SequenceGenerator(name = "room_seq_generator", sequenceName = "room_seq",allocationSize=1)
    @Column(name = "nroom_id", columnDefinition="serial")   
    public Integer nRoomId;

    @Column(name = "ncampus_id")
    public Integer nCampusId;

     //....
     //....

     public Room() {
        super();
    }

Room Controller

@PutMapping("/recoverDeletedRoom")
    public List<Room> recoverRoom(List<Integer> nRoomIdList, Boolean IsActive) {
        return roomService.recoverDeletedRoom(nRoomIdList, IsActive);
    }
SpringUser
  • 1,351
  • 4
  • 29
  • 59
  • List of nRoomIdList coming from UI ? If you are using rest controller can you add code of this; – drowny Oct 03 '18 at 14:50
  • I added controller code – SpringUser Oct 03 '18 at 14:52
  • This doesn't seem to have anything to do with JPQL. The method fails when dealing with a method of your controller, annotated with ModelAttribute (ModelAttributeMethodProcessor) – JB Nizet Oct 03 '18 at 14:58
  • I forgot to add `@RequestParam` in controller class – SpringUser Oct 03 '18 at 15:05
  • it worked or not with adding @requestParam? – drowny Oct 03 '18 at 15:07
  • yes its worked by adding `@RequestParam ` but only when I am adding one value `in nRoomIdList` When I am adding one more value in nRoomIdList I am getting error ` "Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List';` – SpringUser Oct 03 '18 at 15:12
  • 1
    Your spring version is before the 3.0 ? Please try with `@RequestParam Integer[] nRoomIdList` instead of List. And change it to list before sending to roomService. – drowny Oct 03 '18 at 15:25
  • when I am checking result in swagger I am getting expected result. but using same input parameters when I am checking result through postman I am getting ` []` – SpringUser Oct 04 '18 at 07:07

2 Answers2

3

Change your @PutMapping code

@PutMapping("/recoverDeletedRoom")
public List<Room> recoverRoom(@RequestBody WrapperObject wrapperObject) {
   return roomService.recoverDeletedRoom(wrapperObject.getNRoomIdList(), getIsActive());
}

And get put mapping body;

public class WrapperObject {

    List<Integer> nRoomIdList;
    Boolean isActive;

    //getters setters
}
drowny
  • 2,067
  • 11
  • 19
2

You get this exception if your parameters do not have the @RequestBody annotation.

The @RequestBody annotation is only allowed for one parameter that receives the whole body of the request.

So in your case you need a wrapper object.

@PutMapping("/recoverDeletedRoom")
    public List<Room> recoverRoom(@RequestBody YourWrapperObject wrapper) {
        ...
    }
Ari
  • 1,361
  • 1
  • 15
  • 23