SpringUser
SpringUser

Reputation: 1439

Spring Data JPA : java.lang.NoSuchMethodException: java.util.List.<init>()

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);
    }

Upvotes: 1

Views: 8728

Answers (2)

Ari
Ari

Reputation: 1470

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) {
        ...
    }

Upvotes: 2

drowny
drowny

Reputation: 2147

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
}

Upvotes: 3

Related Questions