-1
  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>
Laazo
  • 467
  • 8
  • 22
  • 1
    What does your `persistence.xml` look like? – Robby Cornelissen Jan 25 '17 at 07:42
  • @RobbyCornelissen I have included the `persistence.xml` config – Laazo Jan 25 '17 at 07:47
  • @Azola Where have you declared that class Product contains a mapping? I mean, in one of your configuration files, you must indicate that Product (or its package) got to be managed by the persistence manager. – RubioRic Jan 25 '17 at 07:58
  • You can see an example here http://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml – RubioRic Jan 25 '17 at 07:59
  • 1
    Your `persistence.xml` is invalid. It doesn't have the opening `properties` tag. – Robby Cornelissen Jan 25 '17 at 08:03
  • Can you put " yourpackage.path.Product" to under the "persistence-unit" in persistence.xml ? – GltknBtn Jan 25 '17 at 08:37
  • I ran into something similar when setting-up a second `persistence.xml` in the test resources to be used in the unit tests. It turned-out something about the order of classloading prevented it from finding the classes and I had to list them all as @GltknBtn suggested. Try doing that. – coladict Jan 25 '17 at 08:39

2 Answers2

0

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;
    }
}
user2447161
  • 277
  • 4
  • 12
  • An abstract method needs to be in an abstract class, but this worked for me – Laazo Jan 25 '17 at 08:49
  • Indeed :) The method should be abstract anyway. I would even suggest having a generic interface and a common abstract implementation. Your concrete class would then extend this common abstract implementation. – user2447161 Jan 25 '17 at 09:55
0

Try to put full name instead of simpleClassName.

as below

"FROM " + type.getName()

Anil Agrawal
  • 2,748
  • 1
  • 24
  • 31