Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm getting:
ERROR: column 'id' not found while using session.createNativeQuery() in hibernate 5.4.14


What I have tried:

Java
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);


and there's filed "id" in entity and in a database too but getting:
Java
Caused by: java.sql.SQLException: Column 'id' not found.


Here's entity class:

Java
public class Client  implements java.io.Serializable {    

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


Here's Client.hbm.xml :

Java
<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>


Here's how table is setup in a database https://i.stack.imgur.com/YCC6T.png[^]
Sometimes It's being cause if we import data from dump in a database I tried creating new table in new database but it won't solve.


I'm using MySQL 8.0.21 Hibernate 5.4.14 JDK 11.0.8+10
I won't use JPA Criteria cause I'm checking performance using NativeQuery.
Posted
Updated 3-Sep-20 9:45am
v4
Comments
F-ES Sitecore 3-Sep-20 12:23pm    
Bit of a long shot but all the code samples I've seen have spacing before param name so maybe try that first

select c.fullname, c.city from client c where c.id = :id
Swapnil722 3-Sep-20 12:36pm    
Sorry I've tried but no changes.
Afzaal Ahmad Zeeshan 3-Sep-20 12:42pm    
What is the database structure? Please see that the column "id" exists there.
Swapnil722 3-Sep-20 12:43pm    
id is exist in entity and database too. you can see in linked photo
Swapnil722 3-Sep-20 13:11pm    
It is giving same error that "id" not found for other column fullname. I tried cleaning project and re run but it says "id" not found.

1 solution

Your Client class has Five fields, including one names "id"
But your query has only two fields "Fullname" and "City".

The first column missing in your query is "id".
Make sure the class and query match.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900