0

我收到此错误

menu_categories is not mapped [from menu_categories]

我的休眠电话是

    public List loadMenuCategories(SessionFactory sessionFactory){
    List types = new ArrayList<MenuCategories>();
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    Query query = session.createQuery("from menu_categories");
    List result = query.list(); 
    Iterator it = result.iterator();
    while(it.hasNext()){
        MenuCategories menuCategories = (MenuCategories)it.next();
        types.add(menuCategories);
    }
    sessionFactory.close();
    return types;
}

我的豆子是

@Entity
@Table(appliesTo = "menu_categories")
public class MenuCategories extends BaseModel{

/**
 * 
 */
private static final long serialVersionUID = -4875305890823765933L;

}

package com.rizstien.myhotel.framework.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang.StringUtils;

public class BaseModel implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id", nullable=false)
private Integer id;

@Column(name = "name")
private String name;

@Column(name = "description")
private String desc;

@Column(name = "is_active")
private boolean active;

@Column(name = "no_of_items")
private Integer noOfItems;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    if (!StringUtils.isEmpty(name)) {
        this.name = name;
    }
}
public String getDesc() {
    return desc;
}
public void setDesc(String desc) {
    if (!StringUtils.isEmpty(desc)) {
        this.desc = desc;
    }
}
public boolean isActive() {
    return active;
}
public void setActive(boolean active) {
        this.active = active;
}
public Integer getNoOfItems() {
    return noOfItems;
}
public void setNoOfItems(Integer noOfItems) {
    this.noOfItems = noOfItems;
}

}

编辑 这是我的休眠配置文件

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3311/myhotel</property>
    <property name="hibernate.connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.pool_size">5</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>

    <mapping class="com.rizstien.myhotel.menucategories.model.MenuCategories"/>
</session-factory>

4

2 回答 2

0

您正在执行的查询不是 SQL。这是HQL。HQL 查询实体,而不是表。应该是这样from MenuCategories。顺便说一句,这个实体应该命名为MenuCategory,因为它的一个实例代表一个类别,而不是几个类别。

阅读文档

于 2012-07-09T12:10:15.990 回答
0

我在注释中提到了数据库名称,它解决了问题

 @Entity
 @Table(name = "menu_categories", catalog="db_name")
 public class MenuCategories extends BaseModel{

 private static final long serialVersionUID = -4875305890823765933L;

}
于 2012-07-09T13:51:43.333 回答