【发布时间】:2019-08-31 03:47:00
【问题描述】:
我有一个“Pet”域和一个“Person”域,一个人可能有一只宠物。在我的 RestController 上使用 QuerydslPredicate,我想只返回拥有“DOG”类型宠物的人。
宠物.java
@Entity
public class Pet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Enumerated(EnumType.STRING)
@Column(nullable = false, name = "animal_type")
private AnimalType type;
// constructors & getters/setters
}
enum AnimalType {
DOG, CAT
}
Person.java
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String surname;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(name = "pet_id")
private Pet pet;
// constructors & getters/setters
}
PersonController.java
@RestController
@RequestMapping("persons")
public class PersonController {
@Autowired
private PersonRepository personRepository;
@GetMapping
public List<Person> getAll(@QuerydslPredicate(root = Person.class) Predicate predicate)
{
return (List<Person>) personRepository.findAll(predicate);
}
}
PersonRepository.java
public interface PersonRepository extends JpaRepository<Person, Long>, QuerydslPredicateExecutor<Person> {
}
我只想通过这样的 GET 请求查询养狗的人:
localhost:8080/persons?pet.type=DOG
但这会导致以下错误:
{
"timestamp": "2019-04-09T18:11:23.430+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not access method: Class org.springframework.util.ReflectionUtils can not access a member of class com.example.demo.domain.QPerson with modifiers \"protected\"",
"path": "/persons"
}
使用localhost:8080/persons?name=Tom 查询名为“Tom”的人可以正常工作。
我有这个 repo 可用here。
【问题讨论】: