HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Hibernate / [Solved] Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries

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

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Romi

    July 4, 2013

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

Comments are closed on this article!

Search Tutorials

Hibernate Tutorial

  • Hibernate – Introduction
  • Hibernate – Hello World
  • Hibernate – Get/Fetch
  • Hibernate – Persist
  • Hibernate – Merge & Refresh
  • Hibernate – Get Entity Reference
  • Hibernate – BLOB
  • Hibernate – Save Update
  • Hibernate – Persistence LifeCycle
  • Hibernate – SessionFactory
  • Hibernate – Entities Equality
  • Hibernate – Cascade Types
  • Hibernate – Lazy Loading
  • Hibernate – Criteria Queries
  • Hibernate – HQL
  • Hibernate – Named Query
  • Hibernate – Mappings
  • Hibernate – First Level Cache
  • Hibernate – Second Level Cache
  • Hibernate – EhCache Configuration
  • Hibernate – OSCache Configuration
  • Hibernate – C3P0 Connection Pool
  • Hibernate – In memory Database
  • Hibernate – Bean Validation
  • Hibernate – Validator CDI
  • UnexpectedTypeException

Hibernate Annotations

  • Hibernate – JPA 2 Annotations
  • Annotations Vs Mappings
  • Hibernate – @Immutable
  • Hibernate – @NaturalId
  • Hibernate – @OneToMany
  • Hibernate – @ManyToMany
  • Hibernate – @OneToOne

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces