当前位置: 首页 > 知识库问答 >
问题:

Java Hibernate错误-未映射类

蔺昊穹
2023-03-14

我有一个产品数据库。我已经创建了一个简单的Hibernate项目来从数据库中检索所有的产品。但是,我在编译代码时出现了以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [SELECT p FROM Product p]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:350)
    at databases.ProductsDB.getProducts(ProductsDB.java:36)
    at runnable.TestDriver.main(TestDriver.java:14)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [SELECT p FROM Product p]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
    ... 2 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:332)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 10 more

mysql> describe products;
+---------------+---------------+------+-----+---------+-------+
| Field         | Type          | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| prod_id       | int(11)       | NO   | PRI | NULL    |       |
| prod_name     | varchar(30)   | NO   |     | NULL    |       |
| prod_price    | decimal(10,2) | NO   |     | NULL    |       |
| prod_category | char(5)       | NO   |     | NULL    |       |
+---------------+---------------+------+-----+---------+-------+
@Entity(name = "products")
public class Product implements Serializable {
    @Id
    @Column(name = "prod_id", unique = true)
    private int id;
    @Column(name = "prod_name", nullable = false)
    private String name;
    @Column(name = "prod_price", nullable = false)
    private BigDecimal price;
    @Column(name = "prod_category", nullable = false)
    private String category;

    //skipping the constructor, setters and getters
}
<persistence-unit name="Products_manager" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>products.Product</class>
public static List<Product> getProducts() {
        EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
        String query = "SELECT p FROM Product p WHERE p.id IS NOT NULL";
        TypedQuery<Product> tq = em.createQuery(query, Product.class);
        List<Product> products;
        try {
            products = tq.getResultList();
            return products;
        }
        catch(NoResultException e) {
            e.printStackTrace();
        }
        finally {
            em.close();
        }
        return null;
    }

令人惊讶的是,将prodcuts添加到数据库中可以正常工作。

共有1个答案

甘祺
2023-03-14
@Entity(name = "products")

此名称用于在查询中引用实体

试试看

String query = "SELECT p FROM products p WHERE p.id IS NOT NULL";
 类似资料:
  • 前端将这个json发送到我的API 控制器:

  • 问题内容: 这是错误 我不明白为什么会引发此错误,应该对类进行映射,因为我将简要地向您展示。我有一个非常基本的配置,例如:http : //docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html 我试图将映射定义添加到hibernate.cfg.xml中,并且还尝试以编程方式添加它。他们俩都没有工作。有人可以告诉我我

  • 我试图按照headfirst jsp和servlets的书,我遇到了以下错误。HTTP状态404 - /Beer/form.html 我的文件夹组织如下 假猫- 同样在啤酒文件夹中,我还有另一个文件夹 WEB-INF,其中包含 web.xml 文件。 首先我想也许我的雄猫根本不起作用,所以我使用我以前完成的 servlet 对其进行了测试。成功了。 我重新启动了web服务器,但仍然出现此错误。我正

  • 问题内容: 我试图从“用户”表中获取所有用户的列表,但出现以下错误: 这是我编写的用于添加/获取用户的代码: 添加用户是可行的,但是当我使用getUsers函数时,出现这些错误。 这是我的hibernate配置文件: 这是我的User类: 知道为什么我会收到此错误吗? 问题答案: 在HQL中,应使用映射的 Java类名 和 属性名 ,而不是实际的表名和列名,因此HQL应该为: 更新:更精确地说,您

  • 我使用的是maven release插件,出现以下错误: [错误]无法执行目标组织。阿帕奇。专家plugins:maven release plugin:2.5.3:prepare(默认cli)on project***:“******”的版本未映射- 我从詹金斯那里运行这个任务,源代码在perforce中。我错过了什么?谢谢