1

我正在使用休眠。我正在使用给定的查询从数据库中获取信息

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

我怎样才能正确地做到这一点?谢谢

4

2 回答 2

1

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

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

如果我错了并且它应该是 HQL,您需要发布您的映射 - 它menu_id映射为属性?

于 2011-12-07T00:38:28.193 回答
1

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

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

很可能,您打算查询“menuId”,因为 java bean 在其 getter / Setter 中未使用下划线命名。

于 2011-12-07T00:55:02.410 回答