我有
Product
類;休眠拋出QuerySyntaxException給出正確的類名稱@Entity public class Product { . . public Product() { } . . }
通用的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(); } }
產品DAO class;
public class ProductDao extends BaseDao<Product> { }
一種產品,JAX-RS服務;
@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; } }
當我運行應用程序並調用端點時,我得到一個很好的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>
什麼是你的'persistence.xml'是什麼樣子? –
@RobbyCornelissen我已經包括'persistence.xml' config – Laazo
@Azola你在哪裏聲明瞭類Product包含映射?我的意思是,在你的一個配置文件中,你必須指明產品(或其包)必須由持久性管理器來管理。 – RubioRic