Java org.hibernate.hql.ast.QuerySyntaxException:表名未映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4505035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:07:50  来源:igfitidea点击:

org.hibernate.hql.ast.QuerySyntaxException: TABLE NAME is not mapped

javahibernatejpamany-to-manyplayframework

提问by Amy B

I have two models, Item and ShopSection. They have a many-to-many relationship.

我有两个模型,Item 和 ShopSection。它们是多对多的关系。

@Entity(name = "item")
public class Item extends Model
{
    @ManyToMany(cascade = CascadeType.PERSIST)
    public Set<ShopSection> sections;
}

@Entity(name = "shop_section")
public class ShopSection extends Model
{
    public List<Item> findActiveItems(int page, int length)
    {
        return Item.find("select distinct i from Item i join i.sections as s where s.id = ?", id).fetch(page, length);
    }
}

findActiveItemsis meant to find items in a section, but I get this error:

findActiveItems旨在查找某个部分中的项目,但出现此错误:

org.hibernate.hql.ast.QuerySyntaxException: Item is not mapped [select distinct i from Item i join i.sections as s where s.id = ?]
        at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
        at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
        at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
        at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:322)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
        at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
        at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
        at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
        at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
        ... 8 more

What am I doing wrong?

我究竟做错了什么?

采纳答案by Ian Dallas

You need to add the @Tableannotations like this:

您需要添加这样的@Table注释:

@Entity
@Table(name = "item")
public class Item extends Model
{
    @ManyToMany(cascade = CascadeType.PERSIST)
    public Set<ShopSection> sections;
}

@Entity
@Table(name = "shop_section")
public class ShopSection extends Model
{
    public List<Item> findActiveItems(int page, int length)
    {
        return Item.find("select distinct i from Item i join i.sections as s where s.id = ?", id).fetch(page, length);
    }
}

回答by Ivan Milosavljevic

You need to select from "item" and not from "Item". Entity name is case sensitive.

您需要从“项目”中进行选择,而不是从“项目”中进行选择。实体名称区分大小写。

回答by user2974640

You need to declare Object in hibernate.cgf.xml

您需要在 hibernate.cgf.xml 中声明 Object