JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How to solve QuerySyntaxException (table is not mapped) in hibetnate?

December 1, 2008 by JavaBeat Leave a Comment

The following exception is very common if you are the beginner for hibernate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: book is not mapped [from book]
	at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
	at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
	at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
	at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
	at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
	at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
	at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
	at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77)
	at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56)
	at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
	at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
	at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
	at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
	at hibernate.HibernateExample.main(HibernateExample.java:15)

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: book is not mapped [from book] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623) at hibernate.HibernateExample.main(HibernateExample.java:15)

The above QuerySyntaxException is thrown when we are not mapping the table name properly. The first time hibernate users
can be seen this error because you will map the table name directly in the query. That will not work in the hibernate. You will have to
map the class name that is mapped in the Hibernate configuration file. Look into the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
 
/**
 * source : www.javabeat.net
 */
public class HibernateExample {
    public static void main(String args[]){
        Configuration configuration = new Configuration();
        SessionFactory  sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("delete from Book where id=5");
        query.executeUpdate();
        transaction.commit();
    }
}

package hibernate; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; /** * source : www.javabeat.net */ public class HibernateExample { public static void main(String args[]){ Configuration configuration = new Configuration(); SessionFactory sessionFactory = configuration.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Query query = session.createQuery("delete from Book where id=5"); query.executeUpdate(); transaction.commit(); } }

In the above example Book is the class name that is mapped in the hbm file as follows:

Here the class names is case sensitive.

Filed Under: Hibernate Tagged With: hibernate query

How to use named parameters and named query in Hibernate?

July 26, 2008 by Krishna Srinivasan Leave a Comment

Named Parameters in Hibernate Query

There is two types of query parameters binding in the Hibernate Query. One is positioned parameter and another one is named parameter. But, hibernate recommend to use the named parameters since it is more flexible and powerful compare to the positioned parameter. Here we will look into the named parameter type in detail.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

Named parameters are as name itself suggests, the query string will be using the parameters in the variable name. That can be replaced at runtime and one advantage of using named parameter is, the same named parameter can be used many times in the same query.
[code lang=”java”]
String queryStr = "from Student s where s.name like :searchName";
List result = session.createQuery(queryStr)
.setString("searchName",searchNameValue)
.list;
[/code]

In the above code “:searchName” is the named parameter and it is dynamically added to the query string.

Another good feature is using the named query instead of writing the SQL queries every where. In the named query,all the queries are written inside the .hbm files. Each query is associated with a unique name. Application can load the query by using the name of that query. It avoids writing queries inside the java code itself. The method getNamedQuery() is used for retrieving the query from the mapping file.
[code lang=”java”]
session.getNamedQuery("findStudentByName")
.setString("searchName",searchNameValue)
.list();
[/code]
[code lang=”html”]
<query name="findStudenetbyName"><![CDATA[
from Student s where s.name like :searchName
]]></query>
[/code]

Filed Under: Hibernate Tagged With: hibernate query

Three ways to create query in Hibernate

July 26, 2008 by Krishna Srinivasan Leave a Comment

Create Query in Hibernate

To create query in the Hibernate ORM framework, there is three different types. The following are the
three ways to create query instance:

  1. session.createQuery()
  2. session.createSQLQuery()
  3. session.createCriteria()

We will look into the details of each category in detail.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

session.createQuery()

The method createQuery() creates Query object using the HQL syntax. Fro example
[code lang=”java”]
Query query = session.createQuery("from Student s where s.name
like ‘k%’");
[/code]

session.createSQLQuery()

The method createSQLQuery() creates Query object using the native SQL syntax. Fro example
[code lang=”java”]
Query query = session.createSQLQuery("Select * from Student");
[/code]

session.createCriteria()

The method createCriteria() creates Criteria object for setting the query parameters.
This is more useful feature for those who don’t want to write the query in hand. You can specify any type of complicated syntax using the Criteria API.
[code lang=”java”]
Criteria criteria = session.createCriteria(Student.class);
[/code]

Filed Under: Hibernate Tagged With: hibernate query

Hibernate Criteria Query API / HQL Example

July 23, 2008 by Krishna Srinivasan Leave a Comment

Hibernate Criteria Example

Criteria APIs in the hibernate framework is useful for creating the dynamic query to execute.It is an alternative way to write the queries without using HQL. The queries are generated at runtime and excuted on the fly. Application developer need not worry about writing the query in hand, he/she just need to use APIs provided in the hibernate. It is one of the elegant way to write the queries in hibernate.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

The following example demonstrates very simple hibernate criteria example with retriving 10 results from the table. This is the statement used for creating the Criteria object :

[code lang=”java”]
Criteria criteria = session.createCriteria(Student.class)
[/code]

The above code returns the Criteria for Student class. Once Criteria is created, we can access methods in the API
to set the conditions. In our exampl we called

[code lang=”java”]
criteria.setMaxResults(10);
[/code]

It sets the conditionto retrieve only 10 results.

This is very basic example for the beginner, in the coming sections we will explain how to create the more
complex queries using the Hibernate Criteria API.

JavaBeatHibernateExample.java

[code lang=”java”]
package javabeat.net.hibernate;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
* source : www.javabeat.net
*/
public class JavaBeatHibernateExample {

public static void main(String args[]) {
Configuration configuration = new Configuration();
// configuring hibernate
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Student.class);
criteria.setMaxResults(10);
List<student> list = criteria.list();
for (Student student:list)
{
System.out.println(student.getName());
}
}
}
[/code]

Student.java

[code lang=”java”]
package javabeat.net.hibernate;

/**
* source : www.javabeat.net
*/
public class Student {
private long id;
private String name;
private String dept;
private int marks;
private String section;

public String getDept() {
return dept;
}

public void setDept(String dept) {
this.dept = dept;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public int getMarks() {
return marks;
}

public void setMarks(int marks) {
this.marks = marks;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSection() {
return section;
}

public void setSection(String section) {
this.section = section;
}
}
[/code]

Student.hbm.xml

[code lang=”xml”]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="javabeat.net.hibernate.Student" table="Student_Details">
<id name="id" column="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="dept" column="dept" type="java.lang.String"/>
<property name="marks" column="marks" type="java.lang.Integer"/>
<property name="section" column="section" type="java.lang.String"/>
</class>
</hibernate-mapping>

[/code]

Filed Under: Hibernate Tagged With: hibernate query

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved