user6554564
user6554564

Reputation:

Cannot resolve property

I have prepared a sample application to understand the working of the

Hibernate Criteria Query Language.

I have prepared this using annotations.

All the fields have been specified, with their respective setter-getter methods.

Still, I am getting the following error :

could not resolve property: PRODUCT_PRICE of: com.anno.Product

The following, are my files :

Product.java

@Entity

@Table(name = "products", catalog = "myschema", uniqueConstraints = {

    @UniqueConstraint(columnNames = "productId")})

public class Product {

private int productId;


private String proName;


private double price;



public Product(){}



public Product(int productId, String proName, Double price) {


    this.productId = productId;


    this.proName = proName;


    this.price = price;


}


@Id


@GeneratedValue(strategy = IDENTITY)


@Column(name = "productId", unique = true, nullable = false)


public int getProductId() {


    return productId;


}


public void setProductId(int productId) {


    this.productId = productId;


}


public String getProName() {


    return proName;


}


public void setProName(String proName) {


    this.proName = proName;


}


@GeneratedValue(strategy = IDENTITY)


@Column(name = "price", nullable = false)


public double getPrice() {


    return price;


}


public void setPrice(double price) {


    this.price = price;


}

}

#########################MAIN CLASS
    public class App { 


        @SuppressWarnings("deprecation")


        public static void main(String[] args)


        {


            Configuration cfg = new Configuration();


            cfg.configure("hibernate.cfg.xml"); 


             SessionFactory sf=cfg.buildSessionFactory();  


             Session session=sf.openSession();  



            Criteria crit = session.createCriteria(Product.class);


            Criterion cn = Restrictions.gt("PRODUCT_PRICE",new Double(17000));


            crit.add(cn);


            List<?> l=crit.list();


            System.out.println("List total size..._"+l.size());


            Iterator<?> it=l.iterator();





            while(it.hasNext())


            {

                Product p=(Product)it.next();


                System.out.println(p.getProductId());


                System.out.println(p.getProName());


                System.out.println(p.getPrice());


                System.out.println("-----------------");


            }



            session.close();


            sf.close();


        }

    }

hibernate.cfg.xml

    <?xml version="1.0" encoding="utf-8"?>


    <!DOCTYPE hibernate-configuration PUBLIC


    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"


    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">



    <hibernate-configuration>


    <session-factory>


        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>


        <property 

name="hibernate.connection.url">jdbc:mysql://localhost:3306/myschema

        <property name="hibernate.connection.username">root</property>


        <property name="hibernate.connection.password">admin123</property>


        <property 

name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect

        <property name="show_sql">true</property>


        <mapping class="com.anno.Product" />




    </session-factory>


    </hibernate-configuration>

Upvotes: 1

Views: 1526

Answers (1)

Jens
Jens

Reputation: 69495

You do not work with column names in Hibernate, you work with the property names.

That means you have to Change:

Criterion cn = Restrictions.gt("PRODUCT_PRICE",new Double(17000));

to

Criterion cn = Restrictions.gt("price",new Double(17000));

Upvotes: 0

Related Questions