0

这是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");

Change => "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:产品未映射 [来自产品]

4

0 回答 0