3

This is the first question I have posted here, so apologies in advance if I have violated any conventions or etiquette. I can't seem to get rid of the following exception:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 79 more

Caused by: org.hibernate.HibernateException: Errors in named queries:   User.findByUserNameAndPassword
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)

The entity on which I have my named query looks like this:

@Entity
@Table(name="USERS")
@NamedQueries
({
    @NamedQuery(name="User.findByUserNameAndPassword", query="SELECT u FROM User u   WHERE u.username = :username AND u.password = :password")
})  
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name="USERS_ID_GENERATOR",   sequenceName="PERSONAL.GLOBALSEQUENCE")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USERS_ID_GENERATOR")
private long id;

private String notes;

@Column(name="PASSWORD")
private String password;



@Column(name="USERNAME")
private String username;
...

and I am executing (or trying to!) this query in a UserServiceImpl class

like this:

@Transactional(readOnly=true)
public User authenticate(String userName, String password) {
    List<User> usersList = em.createQuery("User.findByUserNameAndPassword",  User.class).setParameter("username", userName).setParameter("password", password).getResultList();
    User firstUserFromList = usersList.get(0);
    return firstUserFromList;
}

I have tried a lot of things but have been stuck on this for a while now. Any help or guidance will be really appreciated.

Cheers,

My applicationContext.xml file looks like this:


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">      

<context:component-scan base-package="com.transience.sandbox" />
<mvc:annotation-driven />
<tx:annotation-driven />

<mvc:resources mapping="/static_resources/**" location="/static_resources/" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /></property>
    <property name="packagesToScan" value="com.transience.sandbox.domain"/>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>        
    </property>
</bean>    

<jee:jndi-lookup id="dataSource" jndi-name="oracleXEDS"/>
<jpa:repositories base-package="com.transience.sandbox.domain" entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager"/>

</beans>

And I am using Spring (core, webmvc, context, orm, jdbc, tx) 3.1, Hibernate 3.6.8, Spring-data-jpa 1.2.0, Weblogic 12c, and OracleXE. And Maven.

  • show us your config xml file – user123454321 Apr 28 '13 at 08:18
  • @pospeq, I have modified the post with the info – Forever A Noob Apr 28 '13 at 23:37
  • @user1268189 were you able to solve this? Seems like I am stuck at this almost two years leter. Here's my problem: https://groups.google.com/forum/#!topic/play-framework/F3wFUbSYIog Looks like specific to Play framework but am not sure what I might be doing wrong. Let me know if you were able to solve this. – user1242321 Oct 29 '15 at 07:44

2 Answers2

1

Try using em.createNamedQuery() instead of em.createQuery().

David Levesque
  • 22,181
  • 8
  • 67
  • 82
0

Few initial observations :

You have defined entity as USER which is a reserved word in Oracle. Therefore query SELECT u FROM User u ... might create issue when it gets converted into the native query.

Try looking into the generated query by setting appropriate logging level & execute it. Also try to avoid using reserved words/keywords & specify more meaningful conventions.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • The same query works when defined within the Service implementation class `UserServiceImpl` like so: `private static final String QUERY_FINDBYUSERNAMEANDPASSWORD = "select u from User u where u.username = :username and u.password = :password";` Also, with the NamedQuery, I am unable to even deploy the app to WLS – Forever A Noob Apr 29 '13 at 09:32
  • @user1268189 NamedQuery is validated at deployment time, so any issue in it will lead failure in deployment. – Nayan Wadekar Apr 29 '13 at 11:19