java – org. hibernate. hql. ast. Querysyntaxexception: table name is not mapped

I have two models, item and shopsection They have a many - to - many relationship

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

Findactiveitems is to find items in a section, but I get this error:

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 on earth did I do wrong?

Solution

You need to add the following @ table comment:

@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,length);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>