【问题标题】:Spring MVC Application syntax errorSpring MVC 应用程序语法错误
【发布时间】:2015-10-27 16:21:21
【问题描述】:

我有这个问题:

Spring MVC application

我用一些新代码解决了问题,制作了 StudentDeleteRepository.javaStudentDeleteRepositoryImpl.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:无法自动装配字段:私有 com.github.elizabetht.service.StudentDeleteService com.github.elizabetht.controller.StudentDeleteController.studentDeleteService;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“studentDeleteService”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 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 列 [delete s from com.github.elizabetht.model.Student s where 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);

}

【问题讨论】:

    标签: java spring spring-mvc jpa orm


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:
      delete from Student s where s.userName = ?1 and s.password = ?2
      

      【讨论】:

      • 嗨伙计,mmm 带来另一个错误:FactoryBean 在对象创建时抛出异常;嵌套异常是 java.lang.IllegalStateException: Using named parameters for method public abstract com.github.elizabetht.model.Student com.github.elizabetht.repository.StudentDeleteRepository.deleteByLogin(java.lang.String,java.lang.String) 但是在带注释的查询“从学生 s 中删除 s.userName = ?1 和 s.password = ?2”中找不到参数“用户名”!
      • 您可以尝试删除“参数”注释吗?
      • 是的,它可以帮助我的应用程序启动 bean 已正确创建,但当我单击删除按钮时未执行会带来错误
      • :Estado HTTP 500 - 请求处理失败;嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: DML 操作不支持 [从 com.github.elizabetht.model.Student 中删除 s.userName = ?1 和 s.password = ?2];嵌套异常是 java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: DML 操作不支持 [delete from..... ?2]
      • 删除用户部分已完成...对于那些有该错误的人: //@Modifying //@Query("delete from Student s where s.userName = :userName and s.password = :password ") @Transactional int deleteByLogin(@Param("userName") String userName, //@Param("password") String password);解决问题
      猜你喜欢
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2015-12-02
      相关资源
      最近更新 更多