找不到元素'持久性的声明,但是正确设置了xmlns

时间:2014-08-28 23:24:50

标签: java jpa persistence

这让我疯了。

我有一个appContext-model.xml文件,用于导入persistence.xml。

<import resource="classpath:META-INF/persistence.xml" />

根据我对EclipseLink 2.5.1(即JPA 2.0)的了解,我的persistence.xml设置了正确的xmlns详细信息。

 <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

        <persistence-unit name="graps-jpa" transaction-type="RESOURCE_LOCAL">        
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>                
            <class>com.mycompany.rd.model.graps.PrProject</class>       
            <exclude-unlisted-classes>true</exclude-unlisted-classes>        
            <properties>            
                <property name="eclipselink.target-database" value="Oracle"/>            
                <property name="eclipselink.ddl-generation" value="none"/>            
                <property name="eclipselink.weaving" value="static"/>        
            </properties>
        </persistence-unit>  

        <persistence-unit name="xp-jpa" transaction-type="RESOURCE_LOCAL">        
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>                
            <class>com.mycompany.rd.model.misf.Project</class>      
            <exclude-unlisted-classes>true</exclude-unlisted-classes>        
            <properties>            
                <property name="eclipselink.target-database" value="SQLServer"/>            
                <property name="eclipselink.ddl-generation" value="none"/>            
                <property name="eclipselink.weaving" value="static"/>        
            </properties>
        </persistence-unit>
    </persistence>

我的pom加载:

<dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>${eclipselink.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>commonj.sdo</artifactId>
                    <groupId>commonj.sdo</groupId>
                </exclusion>
            </exclusions>
            <scope>compile</scope>
        </dependency>

版本为2.5.1。

我尝试将javax.persistence添加到pom.xml中,但这些似乎都没有帮助:

<!-- 
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.0.0</version>
                <scope>provided</scope>
            </dependency>
         -->

        <!-- 
        <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.0.1.Final</version>
            </dependency>

         -->

但我一直这样说:

cvc-elt.1: Cannot find the declaration of element 'persistence'.

8小时与此搏斗....任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

persistence.xml文件不是spring的一部分,因此您需要删除:

<import resource="classpath:META-INF/persistence.xml" />

创建Spring bean并收到persistence.xml并处理它:

<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManag‌​er">
    <property name="persistenceXmlLocations"> 
        <list> 
            <value>classpath*:META-INF/persistence.xml</value> 
        </list> 
    </property>         
</bean>

另见this