Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

org.hibernate.HibernateException: Errors in named queries  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using JPA with hibernate. Im able to insert record into db using JPA.
when im trying retrieve record from DB using NamedQuery, im getting javax.persistence.PersistenceException: [PersistenceUnit: wsrdatabase] Unable to build EntityManagerFactory
Am i missing something? please help me in that.
Entity bean:
@Table(name = "employee")
@NamedQueries( {
@NamedQuery(name = "employee.findAll", query = "SELECT e FROM employee e"),
@NamedQuery(name = "employee.byId",
query = "SELECT e FROM employee e WHERE e.employeeNum = :empNum") })

public class Employee

@Column(name = "employeeNum", nullable = false, unique = true)
private String employeeNum;

@Column(name = "firstName", nullable = false, unique = false)
private String firstName;
// class using entity bean
public List<Employee> searchAllEmployee() throws EmployeeException
EntityManagerFactory entityManagerFactory = Persistence.
createEntityManagerFactory( Constants.PERSISTENCE_UNIT );
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query findAllQuery = entityManager.createNamedQuery("employee.findAll");
List<Employee> employees = findAllQuery.getResultList();
return employees;
<persistence-unit name="wsrdatabase">

<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/wsrdatabase" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

<property name="hibernate.connection.pool_size" value="6" />
<property name="hibernate.connection.autoReconnect" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="" value="update" />

Full stack trace of exception that occured:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
[PersistenceUnit: wsrdatabase] Unable to build EntityManagerFactory
javax.persistence.PersistenceException: [PersistenceUnit: wsrdatabase] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(
at javax.persistence.Persistence.createEntityManagerFactory(
at javax.persistence.Persistence.createEntityManagerFactory(
at com.wipro.sms.wsr.employee.EmployeeServiceImpl.searchAllEmployee(
at com.wipro.sms.wsr.employee.EmployeeServiceTest.testSearchAllEmployee(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(
at junit.framework.TestCase.runBare(
at junit.framework.TestResult$1.protect(
at junit.framework.TestResult.runProtected(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
Caused by: org.hibernate.HibernateException: Errors in named queries: employee.findAll, employee.byId
at org.hibernate.impl.SessionFactoryImpl.<init>(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(
... 21 more
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using

@NamedQuery(name = "employee.findAll", query = "SELECT e FROM Employee e"),

Employee instead of employee
surendra harikantra
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot buddy it is working now. :lol:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!