Question

I am getting SQL Grammar Exception on simple select statement here is my products class

@Entity
public class products {

    private String Code,Name,Description;
    private double Price;

    @Id
    public String getCode() {
        return Code;
    }
    public void setCode(String code) {
        Code = code;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getDescription() {
        return Description;
    }
    public void setDescription(String description) {
        Description = description;
    }
    public double getPrice() {
        return Price;
    }
    public void setPrice(double price) {
        Price = price;
    }
}

Here is my query which i am executing

public products getProductDescription(String Code)
{

    setSessionFactory();
    Session session = factory.getCurrentSession();

    session.beginTransaction();
    String query = "from products p where p.code=:code";
    Query q = session.createQuery(query);
    q.setString("code", Code);
    System.out.println(query);
    products pr  = (products)session.createQuery(query);
    session.close();
    return pr;
}

All The Tables Exist and "from products" query works perfectly but when i insert where clause here it doesn't work

UPDATE1

public products getProductDescription(String Code)
{

    setSessionFactory();
    Session session = factory.getCurrentSession();

    session.beginTransaction();
    String query = "from products p where p.code=:code";
    Query q = session.createQuery(query);
    q.setString("code", Code);
    System.out.println(query);
    products pr  = (products)session.createQuery(q);
    session.close();
    return pr;
}

Error at products pr = (products)session.createQuery(q); saying The function createQuery does not support argument of type Query. Quickfix tells me to change it to string

Was it helpful?

Solution

Note you should change your property names to start with lower case letter like code instead of Code. That's required when you are working with a lot of frameworks.

Once you have renamed your properties, try the following -

String query = "from products p where p.code=" + Code;

Also, as @Luiggi suggests in the comment always use parameters instead of concatenating strings. e.g.

String query = "from products p where p.code = :code";

Another advice is to make your class name start with upper case letter. e.g. Product instead of products.

Finally, your query would look like -

 String query = "from Product p where p.code = :code";
 Query q = session.createQuery(query);
 q.setString("code", code); 

Update

public products getProductDescription(String Code)
{

    setSessionFactory();
    Session session = factory.getCurrentSession();

    session.beginTransaction();
    String query = "from products p where p.code=:code";
    Query q = session.createQuery(query); //query is already created here
    q.setString("code", Code);
    System.out.println(query);

    //delete the line below
    //products pr  = (products)session.createQuery(q);

    //add the lines below
    List<products> l = (List<products>) q.list();
    products pr = null;
    if(!l.isEmpty()) {
        pr = l.get(0);
    }

    session.close();
    return pr;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top