[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 !!

One thought on “[Solved] Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries”

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

Leave a Reply

Your email address will not be published. Required fields are marked *


five × = 25

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>