gpt4 book ai didi

java - JSF、JPA 应用程序中的 SQL 查询错误

转载 作者:搜寻专家 更新时间:2023-11-01 01:08:09 26 4
gpt4 key购买 nike

我的应用程序中的查询有问题。这是执行查询的查询方法:

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

在跟踪中我发现:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [SELECT * FROM PRODUCT WHERE CATEGORY='Humano']. [22, 22] A select statement must have a FROM clause. [7, 7] The left expression is missing from the arithmetic expression. [9, 22] The right expression is not an arithmetic expression

有什么想法吗?我的目标是刷新网页中的 JSF 数据表。


根据@Ilya 的回答编辑了我的代码,现在我得到了这个异常

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Problem compiling [SELECT p FROM PRODUCT p WHERE CATEGORY='Humano']. [14, 21] The abstract schema type 'PRODUCT' is unknown. [30, 38] The identification variable 'CATEGORY' is not defined in the FROM clause.


应@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();
}
}

最佳答案

1) FROM子句中需要为表指定别名,SELECT子句中应包含别名
Product 应该是一个实体

em.createQuery("SELECT p FROM Product p WHERE p.category='" + category + "'");   

如果 PRODUCT 不是一个实体,你应该创建 nativeQuery

em.createNativeQuery("SELECT p FROM PRODUCT p WHERE p.CATEGORY='" + category + "'");  

EntityManager::createQuery用于 JPQL(Java 持久性查询语言)
EntityManager::createNativeQuery用于 SQL

2) 当 JPA 无法定位您的实体类时,JPA 抛出 "Unknown abstract schema type" 错误
同时将实体添加到 persistence.xml

 <persistence-unit ...>
<class>com.package.Product</class>

3) 添加@Table 注释到您的@Entity
4) 正如我在 documentation 中看到的那样, JPQL 区分大小写。

With the exception of names of Java classes and properties, queries are case-insensitive. So SeLeCT is the same as sELEct is the same as SELECT, but org.hibernate.eg.FOO and org.hibernate.eg.Foo are different, as are foo.barSet and foo.BARSET.

所以JPQL查询应该是

SELECT p FROM Product p WHERE p.category = '...

关于java - JSF、JPA 应用程序中的 SQL 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19412642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com