首页 > 解决方案 > 错误:在 Hibernate 5.4.14 中使用 NativeQuery 时未找到列 'id'

问题描述

我有以下查询:

try (Session session = sessionFactory.openSession()) {
    var clients = session.createNativeQuery("select c.fullname, c.city from client c where c.id=:id")
            .addEntity("c", Client.class)
            .setParameter("id", id);

并且在实体和数据库中也有归档的 id 但得到

`Caused by: java.sql.SQLException: Column 'id' not found.`  

这是实体类:

public class Client  implements java.io.Serializable {    

     private Integer id;
     private Product product;
     private String fullname;
     private String business;
     private String address;  

这是 Client.hbm.xml :

<class name="javafxapplication.pojo.Client" table="client" catalog="clientie" optimistic-lock="version">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>

在此处输入图像描述

我正在使用 MySQL 8.0.21 Hibernate 5.4.14 JDK 11.0.8+10
我不想使用 JPA Criteria 查询因为我正在使用 NativeQuery 来检查性能。
所以不建议使用JPA

标签: javahibernatehibernate-native-query

解决方案


推荐阅读