Hibernate中的查询异常

时间:2011-12-07 00:18:36

标签: java hibernate hql sessionfactory

我正在使用hibernate。我正在使用给定的查询从db

获取信息
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 Class

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"/>

我该如何正确地做到这一点? 感谢

2 个答案:

答案 0 :(得分:1)

该查询看起来像SQL而不是HQL。如果是这种情况,请改用session.createSQLQuery()

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

如果我错了并且它意味着是HQL,那么你需要发布你的映射 - 它menu_id映射为属性?

答案 1 :(得分:1)

未解析的属性就是:hibernate看不到getter / setter的java属性。

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

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