gpt4 book ai didi

java - Hibernate中的查询异常

转载 作者:行者123 更新时间:2023-11-30 04:56:24 25 4
gpt4 key购买 nike

我正在使用 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;
}

}

菜单ID类

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

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

最佳答案

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

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

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

关于java - Hibernate中的查询异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com