I have this question:
I solved with some new code, making StudentDeleteRepository.java and StudentDeleteRepositoryImpl.java and adding the tags as a user suggested:
@Autowired
private StudentDeleteRepository studentDeleteRepository;
@Transactional
public Student delete(Student student) {
return studentDeleteRepository.save(student);
}
StudentDeleteRepository.java gives the error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDeleteController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private 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; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private 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; 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]
Here is the class 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. See below how to write JPA query in where
conditions.
@Param
annotation is used in the Service declaration or implementation, not in the JPA repository implementation. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.