SpringBoot +JPA 自定义查询报错,Column 'xx' not found.

SpringBoot +JPA 自定义查询报错,Column ‘xx’ not found.

本人近期在学习JPA 的路上遇到的小问题。自己定义的查询方法,只想查询某些字段。因为自己了解不深出的问题。同时也希望有大佬做一个底层的解析。不多比比上代码。

项目的整体目录

项目的整体目录

这个是 repository

public interface StudentRepository extends JpaRepository<Student,Integer> {

    List<Student> findByName(String name);

    Student findByClaId(int cid);
    
	//查询id,和name字段
    @Query(value = "select id,name from student_table where id = ?1",nativeQuery = true)
    Student findById(int id);


}

实体类里的Student

@Data
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@EntityListeners(AuditingEntityListener.class)
@Table(name = "student_table") //@Table来指定和哪个数据表对应;如果省略,默认表名就是该类名的小写:user
public class Student {
    @Id //表示这个属性是数据表中的主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) //还是一个自增的主键
    private Integer id;
    private String name;
    private String sex;
    private Double height;
    private String birthday;
    private Integer claId;
    @CreatedDate
    private Date createTime;
    @LastModifiedDate
    private Date updateTime;
    @Version
    private int version;
}

Service 和 他的实现类 Impl

//service 接口
Student findById(int id);

//impl 实现类
	@Autowired
    private StudentRepository studentRepository;
    
	@Override
    public Student findById(int id) {
        return studentRepository.findById(id);
    }

Controller 代码

@ApiOperation(value = "查询名字")
    @GetMapping(value = "/seleById/{id}")
    public Student deleteBatchId(@PathVariable int id){
        System.out.println(id);
        return studentService.findById(id);
    }

用Swagger 去测试

java.sql.SQLException: Column 'birthday' not found.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.mysql.cj.jdbc.result.ResultSetImpl.findColumn(ResultSetImpl.java:548) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:852) ~[mysql-connector-java-8.0.19.jar:8.0.19]
	at com.zaxxer.hikari.pool.HikariProxyResultSet.getString(HikariProxyResultSet.java) ~[HikariCP-3.4.2.jar:na]
	at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:62) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3068) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1866) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.hydrateEntityState(Loader.java:1794) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1767) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.getRow(Loader.java:1615) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:745) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.processResultSet(Loader.java:1008) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.doQuery(Loader.java:964) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.doList(Loader.java:2838) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.doList(Loader.java:2820) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2652) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.Loader.list(Loader.java:2647) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2131) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1163) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:173) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1530) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1578) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	等等

问题:找不到“xxx”列。

问题原因:我写的原生SQL语句里面 只返回

id,name 两个字段 而Student实体类里面有9个属性。

解决方法一:修改查询语句 但是已经偏离我的本意。将字段换成 * 代表所有字段

//查询id,和name字段
    @Query(value = "select * from student_table where id = ?1",nativeQuery = true)
    Student findById(int id);

查询结果:查询成功。
在这里插入图片描述
解决方法二:这个办法很让我吐血。

首先新建一个repository 注意 JpaRepository<StudentDemo,Integer> 实体类我已经更换了。这是我又新建的。

public interface StudentDemoRepository extends JpaRepository<StudentDemo,Integer> {

    @Query(value = "select id,name from student_table where id = ?1",nativeQuery = true)
    StudentDemo findById(int id);
}

新建一个实体类 StudentDemo

@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "student_table") //@Table来指定和哪个数据表对应;如果省略,默认表名就是该类名的小写:user
@Data
public class StudentDemo {

    @Id //表示这个属性是数据表中的主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) //还是一个自增的主键
    private Integer id;
    private String name;
}

service impl controller 因为都很简单一起粘上

//service 
StudentDemo findById(int id);

//impl
@Service
public class StudentServiceImpl implements IStudentService {
	@Autowired
    private StudentDemoRepository studentDemoRepository;

    public StudentDemo findById(int id) {
        return studentDemoRepository.findById(id);
    }
}

//controller
 @ApiOperation(value = "查询名字")
    @GetMapping(value = "/seleById/{id}")
    public StudentDemo deleteBatchId(@PathVariable int id){
        System.out.println(id);
        return studentService.findById(id);
    }

回来运行运行成功,返回数据也是自己想要的。

{
  "id": 5,
  "name": "小黑"
}

如果是想查询数据库返回的一个字段,可以用String Integer 来接收。
这次只是记录自己在学习路上的一个小问题。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
【资源说明】 1、基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的小青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值