0

This is the Exception I get in my code:

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

This part is the problematic code.

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

    Session session = sessionFactory.getCurrentSession();

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

What I have been trying is the following:

1)

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

Change => "from Product" but it does not change Anything

2) Changing

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

but it does not change Anything too!!

This is the full code

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 Status 500 – Internal Server Error java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: product is not mapped [from product]

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of [Hibernate throwing a QuerySyntaxException with correct class name given](https://stackoverflow.com/questions/41845938/hibernate-throwing-a-querysyntaxexception-with-correct-class-name-given) – halfer May 09 '19 at 16:43
  • There are [quite a few possible duplicates](https://stackoverflow.com/search?tab=newest&q=org.hibernate.hql.internal.ast.QuerySyntaxException%3a%20product%20is%20not%20mapped). – halfer May 09 '19 at 16:43

0 Answers0