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

时间:2018-10-03 14:36:14

标签: java spring-data-jpa

我正在使用Spring Data JPA创建服务。我正在尝试在IN clause查询中使用JPQL

实际上,我正在尝试将此LinQ查询转换为JPQL

LinQ查询

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

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

此解决方案不适用于我。在所有模型类中,我都有默认构造函数。

JPQL查询语法

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

控制台

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]

客房等级

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

房间控制器

@PutMapping("/recoverDeletedRoom")
    public List<Room> recoverRoom(List<Integer> nRoomIdList, Boolean IsActive) {
        return roomService.recoverDeletedRoom(nRoomIdList, IsActive);
    }

2 个答案:

答案 0 :(得分:2)

更改您的@PutMapping代码

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

并获得放置映射主体;

public class WrapperObject {

    List<Integer> nRoomIdList;
    Boolean isActive;

    //getters setters
}

答案 1 :(得分:2)

如果您的参数没有@RequestBody注释,则会出现此异常。

仅{strong>一个参数允许使用@RequestBody批注,该参数接收整个请求。

因此,在您的情况下,您需要一个包装对象。

@PutMapping("/recoverDeletedRoom")
    public List<Room> recoverRoom(@RequestBody YourWrapperObject wrapper) {
        ...
    }