我有一个Product
类;
@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类;
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>
答案 0 :(得分:0)
为避免冲突,请在实现类中指定类和实体管理器。示例:
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();
}
然后你的实现类:
public class ProductDao extends BaseDao<Product> {
@PersistenceContext
private EntityManager em;
public ProductDao(){
super(Product.class);
}
/**
* {@inheritDoc}
*/
@Override
protected EntityManager getEntityManager() {
return em;
}
}
答案 1 :(得分:0)
尝试输入全名而不是simpleClassName。
如下
&#34; FROM&#34; + type.getName()