Hibernate throwing a QuerySyntaxException with correct class name given

℡╲_俬逩灬. 提交于 2019-12-14 03:23:47

问题


  1. I have a Product class;

    @Entity
    public class Product {
        .
        .
        public Product() { }
        .
        .
    }
    
  2. A generic DAO;

    public class GenericDao<T> {
    
        private Class<T> type;
    
        @Inject
        protected EntityManager entityManager;
    
        public GenericDao() { }
    
    
        public List<T> list() {
            return entityManager.createQuery("FROM " + type.getSimpleName(), type).getResultList();
        }
    }
    
  3. A Product DAO class;

    public class ProductDao extends BaseDao<Product> { }

  4. A product JAX-RS service;

    @Path("/product")
    public class ProductService {
    
        @Inject
        private ProductDao productDao;
    
        @GET
        @Path("/getProducts")
        @Produces(MediaType.APPLICATION_JSON)
        public List<Product> getProducts() {
            List<Product> response = productDao.list();
            return response;
        }
    }
    

    When I run the app and make a call to the endpoint I get a nice QuerySyntaxException;

    org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [FROM Product]
    

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="mainconfig">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
       <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/awsapp" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="javax.persistence.jdbc.user" value="${conf.jdbc.user}" />
            <property name="javax.persistence.jdbc.password" value="${conf.jdbc.password}" />
            <property name="hibernate.show_sql" value="true" />

        </properties>
    </persistence-unit>

</persistence>

回答1:


To avoid conflicts, specify your Class and entity manager in implementation classes. Example :

public abstract class GenericDao<T> {
    private Class<T> clazz;

    public GenericDao(Class<T> clazz) {
        this.clazz = clazz;
    }

    public T getById(Long key) {
        return getEntityManager().find(clazz, key);
    }
    protected abstract EntityManager getEntityManager();
}

Then your implementation class :

public class ProductDao extends BaseDao<Product> { 
    @PersistenceContext
    private EntityManager em;

    public ProductDao(){
        super(Product.class);
    }

        /**
     * {@inheritDoc}
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}



回答2:


Try to put full name instead of simpleClassName.

as below

"FROM " + type.getName()



来源:https://stackoverflow.com/questions/41845938/hibernate-throwing-a-querysyntaxexception-with-correct-class-name-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!