我的应用程序中的查询有问题。这是执行查询的查询方法:
public List<Product> obtainProductListByCategory(String category)
{
Query query = em.createQuery("SELECT p FROM PRODUCT p WHERE CATEGORY='" + category + "'");
List<Product> ret = query.getResultList();
if (ret == null)
{
return new ArrayList<Product>();
}
else
{
return ret;
}
}
这是错误:javax.ejb.EJBException
在我发现的痕迹中:
原因:java.lang.IllegalArgumentException:在 EntityManager 中创建查询时发生异常:异常描述:语法错误解析 [SELECT * FROM PRODUCT WHERE CATEGORY='Humano']。[22, 22] select 语句必须有 FROM 子句。[7, 7] 算术表达式中缺少左侧表达式。[9, 22] 正确的表达式不是算术表达式
有任何想法吗?我的目标是在我的网页中刷新 JSF 数据表。
根据@Ilya 的回答编辑了我的代码,现在我得到了这个异常
原因:java.lang.IllegalArgumentException:在 EntityManager 中创建查询时发生异常:异常描述:编译问题 [SELECT p FROM PRODUCT p WHERE CATEGORY='Humano']。[14, 21] 抽象模式类型“PRODUCT”未知。[30, 38] FROM 子句中未定义标识变量“CATEGORY”。
根据@Ilya 的要求,我发布了我的Product
课程:编辑:在注释中添加了@Table。
package model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table
@Entity
public class Product implements Serializable, IProduct
{
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private String name;
private int stock;
private float price;
private String category;
private String description;
@Override
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Product()
{
}
public Product(String name, int stock, float price, String category, String description)
{
this.name = name;
this.stock = stock;
this.price = price;
this.category = category;
this.description = description;
}
@Override
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
@Override
public int getStock()
{
return stock;
}
public void setStock(int stock)
{
this.stock = stock;
}
@Override
public int hashCode()
{
return name.hashCode();
}
@Override
public boolean equals(Object object)
{
if (!(object instanceof Product))
{
return false;
}
Product other = (Product) object;
if (name.equals(other.getName()))
{
return true;
}
return false;
}
@Override
public String getCategory()
{
return category;
}
@Override
public String toString()
{
return "Marketv2.model.Product[ name=" + name + " ]";
}
}
感谢你目前的帮助。在这里,我在我的应用程序中发布了另一个查询,该查询运行正常:
public void removeProduct(Product g)
{
Query q = em.createQuery("SELECT x FROM BasketItem x WHERE x.product.name = '" + g.getName() + "'");
List<BasketItem> bItems = q.getResultList();
for (BasketItem i : bItems)
{
em.remove(i);
}
q = em.createQuery("DELETE FROM Product x WHERE x.name = '" + g.getName() + "'");
q.executeUpdate();
}
}