IllegalArgumentException:找不到命名参数 [surname],需要 [name] 之一
IllegalArgumentException: Could not locate named parameter [surname], expecting one of [name]
我的学生资料库class:
private static EntityManagerFactory emf = null;
public EntityManager em() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("teachermanagementPU");
}
EntityManager entityManager = emf.createEntityManager();
return entityManager;
}
public List<StudentEntity> getAll(String name, String surname, Integer age, BigDecimal scholarship) {
String jpql = "select u from StudentEntity u where 1=1";
if (name != null && !name.trim().isEmpty()) {
jpql += "and u.name=:name";
}
if (surname != null && !surname.trim().isEmpty()) {
jpql += "and u.surname=:surname";
}
if (age != null) {
jpql += "and u.age=:age";
}
if (scholarship != null) {
jpql += "and u.scholarship=:scholarship";
}
EntityManager em = em();//JPQL
Query query = em.createQuery(jpql, StudentEntity.class);
if (name != null && name.trim().isEmpty()) {
query.setParameter("name", name);
}
if (surname != null && surname.trim().isEmpty()) {
query.setParameter("surname", surname);
}
if (age != null) {
query.setParameter("age", age);
}
if (scholarship != null) {
query.setParameter("scholarship", scholarship);
}
List<StudentEntity> students = query.getResultList();
em.close();
return students;
}
我收到错误 IllegalArgumentException:无法找到命名参数 [surname],需要 [name] 之一
Students.jsp class:
StudentRepository studentRepository = new StudentRepository();
List<StudentEntity> students = studentRepository.getAll(
request.getParameter("name"),
request.getParameter("surname"),
request.getParameter("age") != null && !request.getParameter("age").isEmpty() ? Integer.parseInt(request.getParameter("age")) : null,
request.getParameter("scholarship") != null && !request.getParameter("scholarship").isEmpty() ? new BigDecimal(request.getParameter("scholarship")) : null);
连接字符串时需要加空格space:
if (name != null && !name.trim().isEmpty()) {
jpql += " and u.name=:name ";
}
其他地方也一样
你还忘了感叹号!
:
if (surname != null && !surname.trim().isEmpty()) {
query.setParameter("surname", surname);
}
你可以使用 isBlank
:
if (surname != null && !surname.isBlank()) {
...
}
我的学生资料库class:
private static EntityManagerFactory emf = null;
public EntityManager em() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("teachermanagementPU");
}
EntityManager entityManager = emf.createEntityManager();
return entityManager;
}
public List<StudentEntity> getAll(String name, String surname, Integer age, BigDecimal scholarship) {
String jpql = "select u from StudentEntity u where 1=1";
if (name != null && !name.trim().isEmpty()) {
jpql += "and u.name=:name";
}
if (surname != null && !surname.trim().isEmpty()) {
jpql += "and u.surname=:surname";
}
if (age != null) {
jpql += "and u.age=:age";
}
if (scholarship != null) {
jpql += "and u.scholarship=:scholarship";
}
EntityManager em = em();//JPQL
Query query = em.createQuery(jpql, StudentEntity.class);
if (name != null && name.trim().isEmpty()) {
query.setParameter("name", name);
}
if (surname != null && surname.trim().isEmpty()) {
query.setParameter("surname", surname);
}
if (age != null) {
query.setParameter("age", age);
}
if (scholarship != null) {
query.setParameter("scholarship", scholarship);
}
List<StudentEntity> students = query.getResultList();
em.close();
return students;
}
我收到错误 IllegalArgumentException:无法找到命名参数 [surname],需要 [name] 之一 Students.jsp class:
StudentRepository studentRepository = new StudentRepository();
List<StudentEntity> students = studentRepository.getAll(
request.getParameter("name"),
request.getParameter("surname"),
request.getParameter("age") != null && !request.getParameter("age").isEmpty() ? Integer.parseInt(request.getParameter("age")) : null,
request.getParameter("scholarship") != null && !request.getParameter("scholarship").isEmpty() ? new BigDecimal(request.getParameter("scholarship")) : null);
连接字符串时需要加空格space:
if (name != null && !name.trim().isEmpty()) {
jpql += " and u.name=:name ";
}
其他地方也一样
你还忘了感叹号!
:
if (surname != null && !surname.trim().isEmpty()) {
query.setParameter("surname", surname);
}
你可以使用 isBlank
:
if (surname != null && !surname.isBlank()) {
...
}