2014-03-14 63 views
4

我試圖讓Spring和Hibernate在沒有persistence.xml的情況下工作。我在我的context.xml文件中設置了我的實體包掃描器,如下所示:錯誤:沒有從{classpath *:META-INF/persistence.xml}解析持久性單元

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/> 
    <property name="hibernateProperties" ref="hibernatePropertiesConfigurer"/> 
    <property name="packagesToScan" value="com.therubythree.simpleapi.entities"/> 
</bean> 

我在想什麼?

我不斷收到錯誤:

No persistence units parsed from {classpath*:META-INF/persistence.xml} 

回答

4

理想的情況下,packagesToScan應該工作。

防爆 -

<property name="packagesToScan" value="tutorials.core.models.entities"></property> 

如果沒有,那麼你可以嘗試這樣的事情。 (根據文檔,這是默認的路徑)

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property> 
    ... 
</bean> 

之後,你應該在META-INF添加persistence.xml中(在src /主/資源)

防爆 -

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
version="1.0"> 
<persistence-unit name="studentPersistenceUnit" transaction-type="RESOURCE_LOCAL" > 
<class>tutorials.models.entities.Student</class> 
</persistence-unit> 
</persistence> 

謝謝

+0

沒有屬性persistenceXmlLocation。你確定嗎? – CuriousMind

+0

@CuriousMind是的,https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html#setPersistenceXmlLocation-java.lang.String- – eis

相關問題