gpt4 book ai didi

java - 由于 @NameQueries 中的错误而无法实例化 LocalContainerEntityManagerFactoryBean?

转载 作者:行者123 更新时间:2023-12-01 13:47:10 26 4
gpt4 key购买 nike

我正在尝试在 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

每当加载网络应用程序时就会出现此问题。我认为错误来自 @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>

最佳答案

这就是命名查询的优点:它们很快就会失败。创建EntityManagerFactory时,会检查所有命名查询,如果其中之一是无效查询,则创建失败。

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

JPQL 使用实体名称及其字段。绝不是表及其列的名称。您选择的 e 与查询中的任何别名都不对应。

查询应该是:

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

关于java - 由于 @NameQueries 中的错误而无法实例化 LocalContainerEntityManagerFactoryBean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20277122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com