Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentS...

本文针对Spring框架中出现的BeanCreationException及TransactionRequiredException异常进行了详细分析,并提供了具体的解决方法,包括正确的查询语句写法及如何正确使用事务注解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/lzx925060109/article/details/40323741

1、

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentServiceBean': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentServiceBean.find(java.lang.String)!

原因:bean类中的扩展查询方法的query语句中的sql语句存在错误。

查询语句应该为select    s   from  Student s where s.id=?1这种样式。其中表明Stduent必须和实体类名完全相同,否则报错。

2、Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query。

原因:事务需求异常。或许是业务逻辑类为注解为@Transactional

样例:

@Repository
@Transactional
public interface StudentServiceBean extends JpaRepository<Student,Long> {
    @Query("select s from Student s where s.username=?1")
    public Student find(String username);
    @Modifying
    @Query("update Student s set s.password=?1 where s.id=?2")
    public int update(String password, Long id);
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值