Zach
Zach

Reputation: 10129

Query Exception in Hibernate

I am using hibernate. I am using the given query to fetch information from 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);

when i run this query I am getting this error

    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]

This is the menu.hbm.xml file

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

Menu Class

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

}

and this is my entry in the cfg file

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

How can I do this properly? Thanks

Upvotes: 1

Views: 229

Answers (2)

jayunit100
jayunit100

Reputation: 17648

Unresolved properties are just that : java properties which hibernate sees no getters/setters for.

In hibernate, your HQL terms must reference the property in the ".cfg" files --- in the data column names.

Most likely, you meant to query for "menuId", since java beans aren't named with underscores in their getter / Setters.

Upvotes: 1

ChssPly76
ChssPly76

Reputation: 100706

That query looks like SQL rather than HQL. If that's the case, use session.createSQLQuery() instead:

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

If I'm wrong and it's meant to be HQL, you'll need to post your mappings - it menu_id mapped as property?

Upvotes: 1

Related Questions