0

I have Route Entity class and when I'm going to fetch all the data from that table in HQL query I get an error for a only one column.

Route.class :-

@Entity
public class Route {

    @Id
    private long routeID;

    private String start;
    private String end;
    private String routeNo;
    private double startLat;
    private double startLon;
    private double endLat;
    private double endLon;
}

DataFetch.Class :-

public class DataFetch{

    public void FetchRoutes(){

        Query q = session.createQuery("select routeID,routeNo,start,end from Route ");
        List<Object[]> routes = (List<Object[]>) q.list();

        for (Object[] r : routes) {
            System.out.println("id: "+r[0]);
            System.out.println("No: "+r[1]);
            System.out.println("start: "+r[2]);
            System.out.println("end: "+r[3]);
        }
    }
}

In DataFetch.class if i remove "end" column from the query q there are no errors. but if i add end again as shown this question,it will give an error. below I show my error as a screen shot.

Click here to see my Error

What is the problem?

4
  • 3
    END being a reserved word? try to quote it, like "end"... Commented Dec 6, 2017 at 10:37
  • 1
    "end" is probably a reserved word. Try escaping it: stackoverflow.com/questions/5686412/… Commented Dec 6, 2017 at 10:39
  • 1
    end is a HQL/SQL Keyword, so you might need to escape it. Commented Dec 6, 2017 at 10:39
  • Thanks for your help! Commented Dec 6, 2017 at 10:45

2 Answers 2

3

End is a reserved keyword so you either escape it or use table alias in select

"select r.routeID, r.routeNo, r.start, r.end from Route r"
Sign up to request clarification or add additional context in comments.

Comments

3

End is probably a reserved word.

But, why not just use Hibernate for what it's worth, and skip the whole manual row mapping thing:

TypedQuery q = em.createQuery("select r from Route r", Route.class);
List<Route> routes = q.getResultList();

Example with em and typed query, since it gives a prettier result. The same can be done with session and a cast..

3 Comments

Thank you very much! I will try typed query! end is a reserved word. thanks again!
You need to use entitymanager instead of sessionfactory though. Might be easiest to just change your select clause to select r, and then the result will be List<Route> (which you will need to cast).
ok.I will try that.I didn't know that method.I'm new to hibernate. Thanks again!

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.