[英]Spring MVC Application syntax error
I have this question: 我有这个问题:
Spring MVC application Spring MVC应用程序
I solved with some new code, making StudentDeleteRepository.java and StudentDeleteRepositoryImpl.java and adding the tags as a user suggested: 我用一些新代码解决了,制作了StudentDeleteRepository.java和StudentDeleteRepositoryImpl.java并根据用户的建议添加了标签:
@Autowired
private StudentDeleteRepository studentDeleteRepository;
@Transactional
public Student delete(Student student) {
return studentDeleteRepository.save(student);
}
StudentDeleteRepository.java gives the error: StudentDeleteRepository.java给出错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDeleteController': Injection of autowired dependencies failed;
org.springframework.beans.factory.BeanCreationException:创建名称为'studentDeleteController'的bean时出错:自动连接依赖项的注入失败; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.github.elizabetht.service.StudentDeleteService com.github.elizabetht.controller.StudentDeleteController.studentDeleteService;
嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有com.github.elizabetht.service.StudentDeleteService com.github.elizabetht.controller.StudentDeleteController.studentDeleteService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDeleteService': Injection of autowired dependencies failed;
嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'studentDeleteService'的bean时出错:自动连接依赖项的注入失败; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.github.elizabetht.repository.StudentDeleteRepository com.github.elizabetht.service.StudentDeleteServiceImpl.studentDeleteRepository;
嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有com.github.elizabetht.repository.StudentDeleteRepository com.github.elizabetht.service.StudentDeleteServiceImpl.studentDeleteRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDeleteRepository': FactoryBean threw exception on object creation;
嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'studentDeleteRepository'的bean时出错:FactoryBean在对象创建时抛出了异常; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: from near line 1, column 10 [delete s from com.github.elizabetht.model.Student s where s.userName = :userName and s.password = :password]
嵌套异常是java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:从第1行第10列[从com.github.elizabetht.model.Student中删除s,其中s.userName = :userName和s.password =:password]
Here is the class StudentDeleteRepositoryImpl.java : 这是StudentDeleteRepositoryImpl.java类:
package com.github.elizabetht.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.github.elizabetht.model.Student;
@Repository("studentDeleteRepository")
public interface StudentDeleteRepository extends JpaRepository<Student, Long> {
@Query("delete s from Student s where s.userName = :userName and s.password = :password")
Student deleteByLogin(@Param("userName") String userName, @Param("password") String password);
}
The query you are using in your code is of Hibernate query, not JPA query. 您在代码中使用的查询是Hibernate查询,而不是JPA查询。 See below how to write JPA query in
where
conditions. 参见下文,在
where
条件下如何编写JPA查询。
@Param
annotation is used in the Service declaration or implementation, not in the JPA repository implementation. @Param
批注用于Service声明或实现中,而不用于JPA存储库实现中。 So change it accordingly. 因此,请相应地进行更改。
@Query("delete s from Student s where s.userName = ?1 and s.password = ?2")
Student deleteByLogin(String userName, String password);
delete from Student s where s.userName = ?1 and s.password = ?2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.