ChiranjeeviIT ChiranjeeviIT - 3 months ago 16
Java Question

"Not mapped" exception while updating the table in Hibernate

I am new to Hibernate, I am trying to update the table column like below, but it's giving an exception - table is not mapped exception.

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlUpdate = "UPDATE TEST_TABLE SET LAST_REFRESH_DATE = :dateToday";
Query query = session.createQuery(hqlUpdate);
query.setParameter("dateToday", new UtilityDate().getTodayDate());
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);


Exception


SEVERE: Servlet.service() for servlet [springmvc] in context with path
[/TEST] threw exception [Request processing failed; nested exception
is org.hibernate.hql.ast.QuerySyntaxException: TEST_TABLE is not
mapped [UPDATE TEST_TABLE SET LAST_REFRESH_DATE = :dateToday]] with
root cause org.hibernate.hql.ast.QuerySyntaxException: TEST_TABLE is
not mapped [UPDATE TEST_TABLE SET LAST_REFRESH_DATE = :dateToday] at
org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at
org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at
org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:94)


I've verified all related questions already in Stack Overflow.

Answer

In hibernate once after defining the table mappings we always communicate with system in terms of Object and Classes. As per the snippet you posted it seems you are using the database table name and its column name in the HQL which is wrong. Instead of this you need to specify the mapped table class name and its property name in place of table column name.

if table TEST_TABLE is mapped with Java class TestTable and lastRefreshDate is the property of this class mapped to LAST_REFRESH_DATE column then below should be the code.

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    String hqlUpdate = "UPDATE TestTable SET lastRefreshDate = :dateToday";
    Query query = session.createQuery(hqlUpdate);
    query.setParameter("dateToday", new UtilityDate().getTodayDate());
    int result = query.executeUpdate();
    System.out.println("Rows affected: " + result);
Comments