gpt4 book ai didi

hibernate - 没有persistence.xml的Spring 3.1给出 "Unable to resolve persistence unit root URL"

转载 作者:IT老高 更新时间:2023-10-28 13:50:01 26 4
gpt4 key购买 nike

我正在尝试使用 JPA EntityManagerFactory bootstrapping without persistence.xml ,这是 Spring 3.1 中的新内容(自 M2 起),但似乎 spring 仍在搜索 persistence.xml 文件,尽管指定了 packagesToScan

在 context.xml 中,entityManagerFactory 是这样配置的:

<bean id="my-persistence-unit"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceProviderClass"
value="org.hibernate.ejb.HibernatePersistence"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>my.package.name</value>
</list>
</property>
</bean>

但在应用程序启动时,会出现 PersistenceException 消息“无法解析持久性单元根 URL”:

...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'my-persistence-unit' defined in class path resource [META-INF/context.xml]:
...
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:429)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:327)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:309)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:209)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
... 34 more
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:426)
... 40 more

在我添加 packagesToScan 之前,我有一个最小的 persistence.xml,并设置 persistenceXmlLocation,如下所示:

<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml"/>

效果很好。那我现在做错了什么?

edit添加了jpaVendorAdapter,错误依旧。

edit 根据已删除的评论删除了 persistenceUnitName;将 bean 重命名为持久性单元名称(对于 @PersistenceContext)

update 这个错误似乎取决于使用哪种应用程序上下文加载器。该代码通过 @ContextConfiguration 在 spring/junit 测试中工作,但不能通过 ClassPathXmlApplicationContext

最佳答案

这是Spring中的一个错误,我reported it .

请注意,当所有类路径项都是 jar 时,它只会发生在打包的应用程序中。这就是它在单元测试中不会失败的原因。

关于hibernate - 没有persistence.xml的Spring 3.1给出 "Unable to resolve persistence unit root URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8052535/

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