JPA读取数据库离谱问题-No property ‘selectClassByName‘ found-Not a managed type

JPA读取数据库离谱问题

报错情况有以下两种:

  • No property 'selectClassByName' found for type 'Cinfo'
  • Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.tang.backend.entity.Cinfo

第一种情况原因:在继承JPA并使用封装好的方法的时候,如果你要自己重新再写一个名字,那么必须和他的一致,如果不一致,你就需要自己重新写@Query注解的内容。

java 复制代码
// 错误写法
Cinfo selectClassById(String class_id);

// 正确写法
@Query(value="select * from class_info where class_id=?1",nativeQuery = true)
Cinfo selectClassById(String class_id);

并且请注意,在写Query里面的SQL语句的时候,如果用到了*号,那么请记得加上后面的nativeQuery = true

第二种情况原因: 继承JPA的时候,后面的泛型需要注意。

java 复制代码
public interface ClassInfoRepository extends JpaRepository<Cinfo, String> {}
// JAP的第一个泛型是你的表格类,第二个泛型是你的表格中主键的类型。
// 因为一般很多项目都是int自增的主键,所以这里很少遇到,但是我的项目是手动设置的String主键,
// 所以这里需要改成String。

当然,我不确定是否还有别的原因导致的,我这里把配置文件中关于JPA的配置贴在下面,希望对各位有帮助。

properties 复制代码
# JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true

# HikariCP
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.register-mbeans=true

# SQL
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
相关推荐
海南java第二人2 分钟前
SpringBoot启动流程深度解析:从入口到容器就绪的完整机制
java·开发语言
问今域中4 分钟前
Spring Boot 请求参数绑定注解
java·spring boot·后端
星火开发设计6 分钟前
C++ queue 全面解析与实战指南
java·开发语言·数据结构·c++·学习·知识·队列
rgeshfgreh8 分钟前
Java+GeoTools+PostGIS高效求解对跖点
java
鱼跃鹰飞9 分钟前
DDD中的防腐层
java·设计模式·架构
计算机程序设计小李同学11 分钟前
婚纱摄影集成管理系统小程序
java·vue.js·spring boot·后端·微信小程序·小程序
栈与堆1 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
一路向北·重庆分伦1 小时前
03-01:MQ常见问题梳理
java·开发语言
一 乐1 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
lhrimperial1 小时前
企业智能知识库助手落地实践:从RAG到Multi-Agent
java·spring cloud·微服务·系统架构·知识图谱