我准备了一个示例应用程序来了解
的工作原理Hibernate Criteria Query Language。
我已经使用注释准备了这个。
已使用各自的setter-getter方法指定了所有字段。
仍然,我收到以下错误:
could not resolve property: PRODUCT_PRICE of: com.anno.Product
以下是我的档案:
Product.java
@Entity
@Table(name =" products",catalog =" myschema",uniqueConstraints = {
@UniqueConstraint(columnNames = "productId")})
公共类产品{
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
名=&#34; hibernate.connection.url&#34;&GT; JDBC:MySQL的://本地主机:3306 / MYSCHEMA
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin123</property>
<property
名=&#34; hibernate.dialect&#34;&GT; org.hibernate.dialect.MySQL5Dialect
<property name="show_sql">true</property>
<mapping class="com.anno.Product" />
</session-factory>
</hibernate-configuration>
答案 0 :(得分:0)
您不使用Hibernate中的列名,而是使用属性名称。
这意味着你必须改变:
Criterion cn = Restrictions.gt("PRODUCT_PRICE",new Double(17000));
到
Criterion cn = Restrictions.gt("price",new Double(17000));