2010-12-21 81 views
9

我有兩個模型,Item和ShopSection。他們有多對多的關係。org.hibernate.hql.ast.QuerySyntaxException:TABLE NAME未映射

@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是爲了找到一個部分項目,但我得到這個錯誤:

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 

我在做什麼錯?

回答

12

您需要添加@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); 
    } 
} 
4

您需要從「項」,而不是從「項目」中選擇。實體名稱區分大小寫。

+0

這是我的問題。這次真是萬分感謝。看起來很明顯,但你並不總是在編寫代碼/表格。 – anon58192932 2013-10-17 19:54:14

0

您需要在hibernate.cgf.xml中聲明對象