Spring MVC Application语法错误

时间:2015-08-05 04:00:47

标签: java spring spring-mvc jpa orm

我有这个问题:

Spring MVC application

我解决了一些新代码,使 StudentDeleteRepository.java StudentDeleteRepositoryImpl.java 并按用户建议添加标记:

@Autowired
private StudentDeleteRepository studentDeleteRepository;


@Transactional
public Student delete(Student student) {
    return studentDeleteRepository.save(student);
}

StudentDeleteRepository.java 给出错误:

  

org.springframework.beans.factory.BeanCreationException:创建名为'studentDeleteController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.github.elizabetht.service.StudentDeleteService com.github.elizabetht.controller.StudentDeleteController.studentDeleteService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'studentDeleteService'的bean时出错:自动连接依赖项的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.github.elizabetht.repository.StudentDeleteRepository com.github.elizabetht.service.StudentDeleteServiceImpl.studentDeleteRepository;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'studentDeleteRepository'的bean时出错:FactoryBean在创建对象时抛出异常;嵌套异常是java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:来自第1行第10列[删除s from com.github.elizabetht.model.Student s其中s.userName = :userName和s.password =:password]

这是 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);

}

2 个答案:

答案 0 :(得分:2)

您在代码中使用的查询是Hibernate查询,而不是JPA查询。 请参阅下文,了解如何在where条件下编写JPA查询。

@Param注释用于Service声明或实现,而不是JPA存储库实现中。所以相应地改变它。

@Query("delete s from Student s where s.userName = ?1 and s.password = ?2")
Student deleteByLogin(String userName, String password);

答案 1 :(得分:1)

delete from Student s where s.userName = ?1 and s.password = ?2