我创建RestController来检索使用JPQL的可分页实体子级。 一切正常,直到我将大小作为参数。
这是我的老师课:
@Entity
public class Teacher {
@Id
private String id;
private String name;
protected Teacher () {}
public String getId() {
return id; }
@ManyToMany
@JoinTable(name = "Teacher_Room", joinColumns = @JoinColumn(name = "Teacher_Id"),
inverseJoinColumns = @JoinColumn(name = "Room_Id"))
List<Room> Rooms = new ArrayList<>();
public void setId(String id) {
this.id = id;}
public String getName() {
return name;}
public void setName(String name) {
this.name = name;}
public List<Room> getRooms() {
return Rooms;}
public void setRooms(List<Room> rooms) {
Rooms = rooms;}
}
房间等级:
@Entity(name="Room")
public class Room {
@Id
private String id;
private String name;
protected Room() { }
public String getName() {
return name;}
public String getId() {
return id;}
public void setId(String id) {
this.id = id;}
public void setName(String name) {
this.name = name;}
}
教师资料库:
@Repository
interface TeacherRepository extends JpaRepository<Teacher, String> {
@Query(value = "SELECT t.Rooms FROM Teacher t where t.id = :id")
public Page<Room> findRooms(String id, Pageable pageable);
}
老师RestController:
@RestController
public class TeacherController {
@Autowired
private TeacherRepository teacherRepository;
@GetMapping("/{id}/rooms")
public Page<Room> getRooms(Pageable pageable, @PathVariable String id) {
Page<Room> pageRooms = teacherRepository.findRooms(id,pageable);
return pageRooms;
}
}
结果:http://localhost:8080/teacher/01/rooms给我正确的结果。
{
"content": [
{
"id": "A",
"name": "Class-A"
},
{
"id": "B",
"name": "Class-B"
},
{
"id": "C",
"name": "Class-C"
},
{
"id": "D",
"name": "Class-D"
},
{
"id": "E",
"name": "Class-E"
},
{
"id": "F",
"name": "Class-F"
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 6,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"numberOfElements": 6,
"first": true,
"empty": false
}
但是如果出现以下错误,请告诉我:http://localhost:8080/teacher/01/rooms?size=3
{
"timestamp": "2019-02-28T19:00:52.796+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not prepare statement; SQL [select count(.) as col_0_0_ from teacher teacher0_
cross join teacher_room rooms1_, room room2_ where teacher0_.id=rooms1_.teacher_id
and rooms1_.room_id=room2_.id and teacher0_.id=?]; nested exception is
org.hibernate.exception.SQLGrammarException: could not prepare statement",
"path": "/01/rooms"
}
我需要建议, 谢谢Jigu