我試圖用JPA EntityManagerFactory bootstrapping without persistence.xml,這是新的春天3.1(因爲M2),但現在看來,彈簧仍在尋求persistence.xml
文件,儘管指定了packagesToScan
。春3.1不persistence.xml中給出了「無法解析持久單元根URL」
在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"/>
哪些工作正常。那麼我現在做錯了什麼?
編輯增加jpaVendorAdapter
,錯誤依然存在。
編輯刪除persistenceUnitName每刪除評論;將其重命名爲持久性單元名稱(對於@PersistenceContext
)
更新此錯誤似乎取決於使用哪種類型的應用程序上下文加載器。代碼通過@ContextConfiguration
進行spring/junit測試,但不通過ClassPathXmlApplicationContext
刪除這個沒有什麼區別;同樣的錯誤。 – beerbajay
@beerbajay:更新。 – axtavt
太棒了,我想我會保持最小的持久性。現在看看他們是如何迴應bug報告的。 – beerbajay