2014-07-17 220 views
0

我正在使用eclipse中的spring jpa hibernate項目。我有一個錯誤在我的persistence.xml文件,但我不知道確切位置:解決方案無效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) 

可以在任何幫助我解決這個問題。

+0

閱讀,似乎一切都OK。我在腳本中唯一的區別(除了映射文件 - 我們使用註釋)是第一行:

+0

您使用Netbeans嗎?如果是,則重命名當前持久性,並生成一個新的比較結果。昨天我用這種方法糾正了一個類似的問題。 –

回答

0

嘗試把映射文件屬性之前:

<?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> 
+0

感謝您的幫助,但現在我得到這個錯誤:'javax.persistence.PersistenceException:無效的persistence.xml。 解析XML時出錯(第1行:第-1列):cvc-complex-type.4:屬性'值'必須出現在元素'property'上。 解析XML時出錯(第1行:第-1列):cvc-complex-type.2.1:由於類型的內容類型爲空,因此元素'屬性'必須沒有字符或元素信息項[children]。 – user3780421

+0

對不起,忘記了關閉屬性「show_sql」 –

+0

我已經在根元素中添加了schemaLocation,你現在可以試試嗎? –