1

I have a problem with hibernate native sql join query. My query is below and works on Mysql db.

SELECT c.cart_id, u.name, u.surname, c.totalPrice
FROM sandbox.cart c JOIN
     sandbox.user u
     ON u.id = c.placedBy

I am using hibernate in code and encountered an exception

java.sql.SQLException: Column 'id' not found.
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093)

Query in code here

Session session = hibernateUtil.getSessionFactory().getCurrentSession();
        SQLQuery query = session.createSQLQuery(ORDER_PER_USER_QUERY);
        query.addEntity(OrderPerUser.class);
        return query.list();

Table column name

Cart

| cart_id | placedBy | totalPrice

User

| id | email | name | surname

My mapped class is

   @Entity
public class OrderPerUser {
@Id
private long id;
private String name;
private String surName;
private long cartId;
private double totalPrice; }
1
  • I already added a mapped class. I added in question Commented Jun 10, 2016 at 13:11

2 Answers 2

1

You need to remove the line:
query.addEntity(OrderPerUser.class);
After that, you need to rewrite the code and map your object manually, because your OrderPerUser is not an entity:

Session session = hibernateUtil.getSessionFactory().getCurrentSession();
    SQLQuery query = session.createSQLQuery(ORDER_PER_USER_QUERY);
    List<OrderPerUser> returnList new ArrayList<>();
    for(Object[] row : query.list()){
        OrderPerUser orderPerUserObj = new OrderPerUser();
        oderPerUserObj.setCartId(Long.parseLong(row[0].toString()));
        //put other properties here
        returnList.add(orderPerUserObj);
    }
    return returnList;

Edit1: Now I see that you added the mapped class, but OrderPerUser should not be an entity in your case, but a regular DTO. An entity requires an ID, but you can't select the ID in this case, because OrderPerUser is not part of a table, it is just some selected data that you want in your memory and not in the database. So you should make your OrderPerUser a regular data transfer object.
Please read about entities, data transfer objects, data access objects to see what each object should do.

Sign up to request clarification or add additional context in comments.

5 Comments

I use OrderPerUser class only mapping the list result. I want to return list only in memory. I dont want to persist db.
I don't persist anything in DB. What I do here, is return a list of the selected information. Nothing is persisted in DB and the returnList will hold the information that you selected and you can then put it in a cache if you want or do other things with it. I basically do exactly what you want to do.
And what error do you have now? The SQLException should not appear anymore because the root of that is that you are using query.addEntity(OrderPerUser.class). Have you removed that line? Have you made the OrderPerUser not an entity, but a normal object?
No I didnt remove. Btw if I change column value in query like this --> SELECT u.id, u.name, u.surname, c.cart_id, c.totalPrice. Exception change to Column 'cartId' not found. Is it about mapping problem ?
You have to understand what ORM does and what ORM does not, before doing happy mappings all the way. In your case, entities are not a solution. You don't have a table represents a OrderPerUser. OrderPerUser is just in your application, a data transfer object, but not an entity. You need to stop mapping to ORM entities when it is not the case.
0

My guess is that your OrderPerUser class which you try to use for collecting the result is expecting a column with name id, and you have no such column in your query...

Try using the query:

SELECT u.id, c.cart_id, u.name, u.surname, c.totalPrice
FROM sandbox.cart c 
     JOIN sandbox.user u ON u.id = c.placedBy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.