Table is not mapped error in ninjaframework

Table is not mapped error in ninjaframework

我正在将 ninjaframework 用于一个简单的网络应用程序。

我在 mysql 中有以下 table:

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    |       |
+----------+---------------+------+-----+---------+-------+

在 My 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

我有一个事件 class 看起来像这样:

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

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

能否在事件 class 之前添加 @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 = "";
    }