Reputation: 123
I have a database of products. I have created a simple Hibernate project to retrieve all the products from the database. However, I got the following exception while compiling the code:
Product is not mapped
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [SELECT p FROM Product p]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:350)
at databases.ProductsDB.getProducts(ProductsDB.java:36)
at runnable.TestDriver.main(TestDriver.java:14)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [SELECT p FROM Product p]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
... 2 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:332)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 10 more
The class Product is mapped indeed - at least that's how I see it.
MySQL database
mysql> describe products;
+---------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| prod_id | int(11) | NO | PRI | NULL | |
| prod_name | varchar(30) | NO | | NULL | |
| prod_price | decimal(10,2) | NO | | NULL | |
| prod_category | char(5) | NO | | NULL | |
+---------------+---------------+------+-----+---------+-------+
Java class Product
@Entity(name = "products")
public class Product implements Serializable {
@Id
@Column(name = "prod_id", unique = true)
private int id;
@Column(name = "prod_name", nullable = false)
private String name;
@Column(name = "prod_price", nullable = false)
private BigDecimal price;
@Column(name = "prod_category", nullable = false)
private String category;
//skipping the constructor, setters and getters
}
.xml file
<persistence-unit name="Products_manager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>products.Product</class>
Affected method
public static List<Product> getProducts() {
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
String query = "SELECT p FROM Product p WHERE p.id IS NOT NULL";
TypedQuery<Product> tq = em.createQuery(query, Product.class);
List<Product> products;
try {
products = tq.getResultList();
return products;
}
catch(NoResultException e) {
e.printStackTrace();
}
finally {
em.close();
}
return null;
}
Surprisingly, adding the prodcuts to the database works fine.
Upvotes: 0
Views: 483
Reputation: 2028
Your code will work fine if you declare your entity class like this:
@Entity
@Table(name = "products")
public class Product implements Serializable {
@Id
@Column(name = "prod_id", unique = true)
private int id;
//...
//...
}
For better understanding, you can have a look name attribute in @Entity and @Table
Upvotes: 1
Reputation: 267
To verify whether table name should be "product" or products , here is what can be done .
use below line hibernate settings.
hibernate.hbm2ddl.auto= create
This will automatically create the tables in testdb . which will help you to understand whether "product" or "products" is correct.
String query = "SELECT p FROM product p WHERE p.id IS NOT NULL";
has to be changed to
String query = "SELECT p FROM products p WHERE p.id IS NOT NULL";
Upvotes: 1
Reputation: 21
@Entity(name = "products")
This name is used to refer to the entity in queries
try
String query = "SELECT p FROM products p WHERE p.id IS NOT NULL";
Upvotes: 1