忍者框架中的表未映射错误

Posted

技术标签:

【中文标题】忍者框架中的表未映射错误【英文标题】:Table is not mapped error in ninjaframework 【发布时间】:2015-06-02 07:58:44 【问题描述】:

我正在为一个简单的 web 应用程序使用 ninjaframework。

我在mysql中有下表:

mysql> desc Event;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| event_id | bigint(20)    | YES  |     | NULL    |       |
| owner_id | bigint(20)    | YES  |     | NULL    |       |
| url1     | varchar(2000) | YES  |     | NULL    |       |
| url2     | varchar(2000) | YES  |     | NULL    |       |
| url3     | varchar(2000) | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+

在我的 DAO 中,我有以下代码:

TypedQuery<Event> query= entityManager.createQuery("SELECT x FROM Event x WHERE event_id = 1", Event.class);

这会导致以下错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Event 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:331)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 43 more

我确实有一个看起来像这样的事件类:

public class Event 
    private int event_id;
    private int owner_id;
    private String url1;
    private String url2;
    private String url3;
    public Event () 
        event_id = -1;
        owner_id = -1;
        url1 = "";
        url2 = "";
        url3 = "";
    

    public Event(int event_id, int owner_id, String url1, String url2, String url3) 
        this.event_id = event_id;
        this.owner_id = owner_id;
        this.url1 = url1;
        this.url2 = url2;
        this.url3 = url3;
    
    public int getEventId() 
        return event_id;
    
    public void setEventId(int id) 
        this.event_id = id;
    
    public int getOwnerId() 
        return owner_id;
    
    public void setOwnerId(int id) 
        this.owner_id = id;
    
    public String getUrl1() 
        return url1;
    
    public void setUrl1(String url) 
        this.url1 = url;
    

    public String getUrl2() 
        return url2;
    
    public void setUrl2(String url) 
        this.url2 = url;
    

    public String getUrl3() 
        return url3;
    
    public void setUrl3(String url) 
        this.url3 = url;
    

    @Override
    public String toString() 
        return getEventId() + ":" + getUrl1() + ":" + getUrl2() + ":" + getUrl3();
    
    ;

为什么会出现此错误? *** 上的大多数线程建议我应该修改休眠配置。但是,我没有任何休眠配置文件,因为 ninja 不需要它。

【问题讨论】:

能否请您在 Event 类之前添加 @Entity(name = "Event") 并导入 javax.persistence.Entity; @erhun 谢谢。成功了! 你能接受我的回答吗,这样问题就可以结束了。 【参考方案1】:

能否请您在 Event 类之前添加 @Entity(name = "Event") 并导入 javax.persistence.Entity;

@Entity(name = "Event")
public class Event 
    private int event_id;
    private int owner_id;
    private String url1;
    private String url2;
    private String url3;
    public Event () 
        event_id = -1;
        owner_id = -1;
        url1 = "";
        url2 = "";
        url3 = "";
    

【讨论】:

以上是关于忍者框架中的表未映射错误的主要内容,如果未能解决你的问题,请参考以下文章

Big Query 中的表未分区

Hibernate表未映射但存在表

忍者中的词法错误是啥意思?

Hibernate中的ManyToMany映射引用错误(由api生成的表名)表Spring Boot

表未从对象数组中正确填充

带有 typescript 的 antd:列 align='right' 的表未编译