由于@NameQueries中的错误,无法实例化LocalContainerEntityManagerFactoryBean?

时间:2013-11-29 01:39:35

标签: java jpa

我正在尝试在JPA中创建一个NamedQuery。不幸的是,由于以下原因,它失败了:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/datasource.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2220)
    ... 1 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:287)
    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)
    ... 14 more
Caused by: org.hibernate.HibernateException: Errors in named queries: searchByUsername
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:489)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    ... 20 more

每当加载Web应用程序时都会出现此问题。我认为错误来自@NamedQuery,如果我删除它,它会正常工作。下面是POJO类的代码片段。

@NamedQueries({
    @NamedQuery(
        name="searchByUsername",
        query="select e from users u where u.username in (select a.username from authorities a where a.username = :username)"
    )
})
@Entity
@Table(name="users")
public class User {
    @Transient
    private String firstName;

    @Transient
    private String lastName;

    @Id
    @Column(name="username")
    private String username;

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

    @Column(name="enabled")
    private Boolean enabled;

    @Transient
    private Role role;
}

这是负责对查询进行调用的DAO类:

@Repository
public class UserDao {
    @PersistenceUnit
    private EntityManagerFactory emf;

    public User findByUsername(String username) {
        try {
            User l = (User) emf.createEntityManager().createNamedQuery("searchByUsername", org.huahsin.WebEngineering.User.class).setParameter("username", username).getResultList();

               ...
            }
            ...

我不确定我是否错过了Spring中的任何配置,我只是把它放在这里供你参考:

    

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="abcd"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="org.huahsin" />
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
    <property name="showSql" value="true" />
</bean>

1 个答案:

答案 0 :(得分:1)

这是命名查询的优势:它们快速失败。创建EntityManagerFactory时,将检查所有命名查询,如果其中一个查询无效,则创建失败。

这允许立即检测查询中的错误,而不是等待它们在运行时执行。

JPQL使用实体名称及其字段。从来没有表格及其列的名称。而且您选择e,这与查询中的任何别名都不对应。

查询应为:

select u from User u where u.username in (select a.username from Authority a where a.username = :username)