【问题标题】:Cannot find the declaration of element 'persistence, but have xmlns set up correctly找不到元素“持久性”的声明,但正确设置了 xmlns
【发布时间】:2014-08-28 23:24:50
【问题描述】:

这让我发疯了。

我有一个导入 persistence.xml 的 appContext-model.xml 文件。

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

据我所知,我的 persistence.xml 已针对 EclipseLink 2.5.1(即 JPA 2.0)设置了正确的 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 小时与此搏斗......任何帮助表示赞赏。

【问题讨论】:

  • 不需要在spring xml文件中导入持久化,见this
  • 不确定我是否理解。我有 2 个实体管理器工厂,并希望将它们放在单独的 persistence.xml 文件中(以避免具有多个 EM 的明显问题)。我现在不想迁移到 JavaConfig。
  • 不,错误是spring不理解你的persistence.xml,删除&lt;import resource="classpath:META-INF/persistence.xml" /&gt;persistence.xml不是一个有效的Spring XML配置。
  • 谢谢,但我如何定义我的 2 个持久单元?我在设置它时遇到了问题,拥有一个单独的 persistence.xml 文件似乎是一种解决方案。
  • 删除导入并改用它似乎可行:classpath*:META-INF/persistence.xml

标签: java jpa persistence


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2014-01-16
    • 2012-01-08
    • 2017-02-06
    • 2016-03-20
    • 2016-09-09
    • 2020-11-09
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多