Prabuddha Kulatunga
Prabuddha Kulatunga

Reputation: 147

unexpected token error in hibernate with Java

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?

Upvotes: 0

Views: 2709

Answers (2)

Zeromus
Zeromus

Reputation: 4542

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"

Upvotes: 3

Tobb
Tobb

Reputation: 12225

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

Upvotes: 3

Related Questions