[Solved] Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries

While working on post “Named queries in hibernate“, I encountered this error. Hibernate entity I coded was like this:

@Entity
@Table(name = "DEPARTMENT", uniqueConstraints = {
					@UniqueConstraint(columnNames = "ID"),
					@UniqueConstraint(columnNames = "NAME") })
@NamedQueries
(
	{
		@NamedQuery(name=DepartmentEntity.GET_DEPARTMENT_BY_ID, query=DepartmentEntity.GET_DEPARTMENT_BY_ID_QUERY),
		@NamedQuery(name=DepartmentEntity.UPDATE_DEPARTMENT_BY_ID, query=DepartmentEntity.UPDATE_DEPARTMENT_BY_ID_QUERY)
	}
)
public class DepartmentEntity implements Serializable {

	static final String GET_DEPARTMENT_BY_ID_QUERY = "from Department d where d.id = :id"; 
	public static final String GET_DEPARTMENT_BY_ID = "GET_DEPARTMENT_BY_ID"; 
	
	static final String UPDATE_DEPARTMENT_BY_ID_QUERY = "UPDATE Department d SET d.name=:name where d.id = :id"; 
	public static final String UPDATE_DEPARTMENT_BY_ID = "UPDATE_DEPARTMENT_BY_ID"; 
	
	//More code
}

And the error was:

Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries: GET_DEPARTMENT_BY_ID, UPDATE_DEPARTMENT_BY_ID
Exception in thread "main" java.lang.ExceptionInInitializerError
	at hibernate.test.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
	at hibernate.test.HibernateUtil.<clinit>(HibernateUtil.java:10)
	at hibernate.test.TestHibernateNamedQuery.main(TestHibernateNamedQuery.java:13)
Caused by: org.hibernate.HibernateException: Errors in named queries: GET_DEPARTMENT_BY_ID, UPDATE_DEPARTMENT_BY_ID
	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:364)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
	at hibernate.test.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
	... 2 more
Random exceptions

Random exceptions

Solution:

The problem is with entity name in most of the cases. Actually we have to follow HQL syntax, and it tells us to use “entity name” in query. So the correct queries are:

static final String GET_DEPARTMENT_BY_ID_QUERY = "from DepartmentEntity d where d.id = :id"; 
static final String UPDATE_DEPARTMENT_BY_ID_QUERY = "UPDATE DepartmentEntity d SET d.name=:name where d.id = :id"; 

Note that I replaced “Department” with “DepartmentEntity”. Now everything works fine.

Happy Learning !!

Lokesh

I have 7 Years of rich experience in java technology. This has only increased my hunger to learn more. In this blog, i will be writing on different topics occasionally, and would love to engage in some meaningful serious discussions with you folks.

You may also like...

1 Response

  1. Romi says:

    The thing is, your query should be in form of HQL. And do not confuse with SQL. That’s it.

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear/post as intended.


Want to ask any question? Or suggest anything?