org.hibernate.hql.internal.ast.QuerySyntaxException:未映射表(来自表X)

时间:2019-05-09 12:17:10

标签: hibernate-mapping spring-framework-beans

这是我在代码中得到的Exception

org.hibernate.hql.internal.ast.QuerySyntaxException: product is not mapped [from Product]

这是有问题的代码。

@SuppressWarnings("unchecked")
public List<Product> getProducts() {

    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery("from product");
    List<Product> productList = query.list();
}

我一直在尝试以下内容:

1)

Query query = session.createQuery("from product");

更改=>“来自产品” 却什么都不会改变

2)更改

List<Product> result = (List<User>) session.createQuery("from Product").list();
session.getTransaction().commit();
return result;

但是它也不会改变任何东西!

这是完整的代码

package kr.ac.hansung.cse.dao;

import java.util.List;    
import org.hibernate.query.Query;    
import org.hibernate.Session;    
import org.hibernate.SessionFactory;    
import org.springframework.beans.factory.annotation.Autowired;    
import org.springframework.stereotype.Repository;    
import org.springframework.transaction.annotation.Transactional;    
import kr.ac.hansung.cse.model.Product;

@Repository    
@Transactional    
public class ProductDao {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    public List<Product> getProducts() {

        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from product");
        List<Product> productList = query.list();

        return productList;
    }

    public Product getProductById(int id) {
        Session session = sessionFactory.getCurrentSession();
        Product product = (Product) session.get(Product.class, id);

        return product;
    }

    public void addProduct(Product product) {

        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(product);
        session.flush(); 

    }

    public void deleteProduct(Product product) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(product);
        session.flush();
    }

    public void updateProduct(Product product) {

        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(product);
        session.flush(); 

    }

}

HTTP状态500 –内部服务器错误 java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:产品未映射(来自产品)

0 个答案:

没有答案