【问题标题】:resolve Invalid persistence.xml解决无效的persistence.xml
【发布时间】:2014-07-17 15:08:37
【问题描述】:

我正在 Eclipse 中使用 spring jpa hibernate 进行项目。我的 persistence.xml 文件有错误,但我不知道具体在哪里:

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="show_sql">true</property>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
        <mapping-file>META-INF/orm.xml</mapping-file>

        </persistence-unit>
</persistence>

我收到这样的错误:

javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-complex-type.4: Attribute 'value' must appear on element 'property'.
Error parsing XML (line-1 : column -1): cvc-complex-type.2.1: Element 'property' must have no character or element information item [children], because the type's content type is empty.
Error parsing XML (line-1 : column -1): cvc-complex-type.2.4.d: Invalid content was found starting with element 'mapping-file'. No child element is expected at this point.

    at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:145)
    at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:169)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:317)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:46)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:30)
    at org.krams.repository.CustomerDAO.addCustomer(CustomerDAO.java:38)
    at org.krams.repository.CustomerDAO$$FastClassByCGLIB$$60d23388.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)

能否帮我解决这个问题。

【问题讨论】:

  • 在阅读时,一切似乎都很好。我在脚本中的唯一区别(除了映射文件——我们使用注释)是第一行: java.sun.com/xml/ns/persistence" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:schemaLocation="@ 987654323@java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
  • 您在使用 Netbeans 吗?如果是,则重命名当前持久性,并生成一个新的,以进行比较。昨天我用这种方法纠正了一个类似的问题。

标签: spring hibernate spring-mvc jpa


【解决方案1】:

尝试将映射文件放在属性之前:

<?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="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <mapping-file>META-INF/orm.xml</mapping-file>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="show_sql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
        </persistence-unit>
</persistence>

【讨论】:

  • 感谢您的帮助,但现在我收到此错误:javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML (line-1 : column -1): cvc-complex-type.4: Attribute 'value' must appear on element 'property'. Error parsing XML (line-1 : column -1): cvc-complex-type.2.1: Element 'property' must have no character or element information item [children], because the type's content type is empty
  • 我在根元素中添加了schemaLocation,你现在可以试试吗?
猜你喜欢
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
  • 2020-04-21
  • 1970-01-01
相关资源
最近更新 更多