2011-12-07 169 views
1

我正在使用hibernate。我使用的是给定的查询,以便从数据库休眠时查询异常

Query q = session.createQuery("select m.menuId,m.menuType,it.itemId,it.name,it.price,it.currency," + 
     "ingr.ingredientId,ingr.ingredient from Menu as m, MenuItem as it," + 
     "KeyIngredient as ingr where m.menuId in "+ 
     "(select MenuId from MenuItem as itm innerjoin KeyIngredient as ing "+ 
     "where itm.itemId = ing.MenuItemId) and m.RestaurantId=" +restaurantId); 

当我运行此查询我收到此错误信息获取

could not resolve property: menuId of: com.hibernate.model.Menu [select m.menuId,m.menuType,it.itemId,it.name,it.price,it.currency,ingr.ingredientId,ingr.ingredient 
from com.hibernate.model.Menu as m, com.hibernate.model.MenuItem as it,com.hibernate.model.KeyIngredient as ingr where m.menuId in (select MenuId from 
com.hibernate.model.MenuItem as itm innerjoin KeyIngredient as ing where itm.itemId = 
ing.MenuItemId) and m.RestaurantId=1] 

这是menu.hbm.xml文件

<hibernate-mapping> 
     <class name="com.hibernate.model.Menu" table="Menu" catalog="mydb"> 
      <composite-id name="id" class="com.hibernate.model.MenuId"> 
       <key-property name="menuId" type="int"> 
        <column name="menu_id" /> 
       </key-property> 
       <key-property name="restaurantId" type="long"> 
        <column name="Restaurant_id" /> 
       </key-property> 
       <key-property name="menuType" type="string"> 
        <column name="menuType" length="45" /> 
       </key-property> 
      </composite-id> 
     </class> 
    </hibernate-mapping> 

菜单类

public class Menu implements java.io.Serializable { 

    private MenuId id; 

    public Menu() { 
    } 

    public Menu(MenuId id) { 
     this.id = id; 
    } 

    public MenuId getId() { 
     return this.id; 
    } 

    public void setId(MenuId id) { 
     this.id = id; 
    } 

} 

MENUID类

public class MenuId implements java.io.Serializable { 

    private int menuId; 
    private long restaurantId; 
    private String menuType; 

    public MenuId() { 
    } 

    public MenuId(int menuId, long restaurantId, String menuType) { 
     this.menuId = menuId; 
     this.restaurantId = restaurantId; 
     this.menuType = menuType; 
    } 

    public int getMenuId() { 
     return this.menuId; 
    } 

    public void setMenuId(int menuId) { 
     this.menuId = menuId; 
    } 

    public long getRestaurantId() { 
     return this.restaurantId; 
    } 

    public void setRestaurantId(long restaurantId) { 
     this.restaurantId = restaurantId; 
    } 

    public String getMenuType() { 
     return this.menuType; 
    } 

    public void setMenuType(String menuType) { 
     this.menuType = menuType; 
    } 

    public boolean equals(Object other) { 
     if ((this == other)) 
      return true; 
     if ((other == null)) 
      return false; 
     if (!(other instanceof MenuId)) 
      return false; 
     MenuId castOther = (MenuId) other; 

     return (this.getMenuId() == castOther.getMenuId()) 
       && (this.getRestaurantId() == castOther.getRestaurantId()) 
       && ((this.getMenuType() == castOther.getMenuType()) || (this 
         .getMenuType() != null 
         && castOther.getMenuType() != null && this 
         .getMenuType().equals(castOther.getMenuType()))); 
    } 

    public int hashCode() { 
     int result = 17; 

     result = 37 * result + this.getMenuId(); 
     result = 37 * result + (int) this.getRestaurantId(); 
     result = 37 * result 
       + (getMenuType() == null ? 0 : this.getMenuType().hashCode()); 
     return result; 
    } 

} 

,这是我在CFG文件

<mapping resource="com/hibernate/model/Menu.hbm.xml"/> 

我怎样才能做到这一点适当的条目? 谢谢

+1

你可以发布你的实体类Menu吗?这听起来像你正在使用数据库ID而不是属性名称。 – CodeSlinger

+0

发布课程。你能弄清楚什么吗? – Zach

回答

1

该查询看起来像SQL而不是HQL。如果是这样的情况下,使用session.createSQLQuery()代替:

Query q = session.createSQLQuery("your SQL here"); 

如果我错了,它的意思是HQL,你需要来发表您的映射 - 它menu_id映射为财产?

+0

我已经为该问题添加了更多信息。你能看看吗? menu_id之前没有映射,但现在我改变了它,但仍然有错误。 – Zach

+1

@Zach你的查询没有太大意义,说实话 - 特别是嵌套子查询。除此之外,由于'menuId'是组合ID的一部分,因此您必须将其引用为:'m.id.menuId'而不是'm.menuId'。 'menuType'也一样# – ChssPly76

+0

好吧,现在我正在得到我出错的地方。我是休眠的新手。你的答案createSQLQuery适合我。谢谢。我想知道如何使用映射文件,这就是为什么我添加更多信息。非常感谢。 – Zach

1

未解决的属性就是:hibernate看不到getters/setters的java属性。

在hibernate中,您的HQL术语必须引用数据列名称中“.cfg”文件中的属性。

最有可能的是,你打算查询“menuId”,因为java bean没有在其getter/Setters中用下划线命名。

+0

SQL vs HQL问题也在其他答案中详细说明.... – jayunit100

+0

我已编辑该问题以添加更多信息。你可以看看。同时我也改变了基于getters和setter的领域 – Zach