3

这是我在这里发布的第一个问题,如果我违反了任何惯例或礼仪,请提前道歉。我似乎无法摆脱以下异常:

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)

我有我的命名查询的实体如下所示:

@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;
...

我正在 UserServiceImpl 类中执行(或尝试!)这个查询

像这样:

@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;
}

我已经尝试了很多东西,但现在已经坚持了一段时间。任何帮助或指导将不胜感激。

干杯,

我的 applicationContext.xml 文件如下所示:

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

我正在使用Spring (core, webmvc, context, orm, jdbc, tx) 3.1, Hibernate 3.6.8, Spring-data-jpa 1.2.0, Weblogic 12c, 和OracleXE. 还有马文。

4

2 回答 2

1

尝试使用em.createNamedQuery()而不是em.createQuery().

于 2013-04-28T17:32:05.540 回答
0

一些初步的观察:

您已将实体定义为USEROracle 中的保留字。因此,查询SELECT u FROM User u ...在转换为本机查询时可能会产生问题。

尝试通过设置适当的日志记录级别来查看生成的查询并执行它。还要尽量避免使用保留字/关键字并指定更有意义的约定。

于 2013-04-29T03:58:21.550 回答