2012-09-05 19 views
9

Kiedy instancji Hibernate stosując następujący kod:Hibernate: Dokument jest nieprawidłowy: gramatyka nie znaleziono

 Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new ServiceRegistryBuilder().applySettings(
       configuration.getProperties()).buildServiceRegistry(); 
     sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

mam następujące wyjątki:

org.xml.sax.SAXParseException: dokument jest invalid: nie znaleziono gramatyki., org.xml.sax.SAXParseException: element główny dokumentu "konfiguracja hibernacji", musi być zgodny z DOCTYPE root "null". Powodowane przez: org.hibernate.MappingException: nieprawidłowa konfiguracja w org.hibernate.cfg.Configuration.doConfigure (Configuration.java:2014) at org.hibernate.cfg.Configuration.configure (Configuration.java:1931) at org.hibernate.cfg.Configuration.configure (Configuration.java:1910) na com.soccer.system.HibernateUtil. (HibernateUtil.java:23) ... 1 więcej Wywołany przez: org.xml.sax.SAXParseException : Dokument jest nieprawidłowy: nie znaleziono gramatyki.

używam następujący hibernate.cfg.xml plik z Hibernate 4.1.6:

<?xml version='1.0' encoding='utf-8'?> 

<hibernate-configuration 
    xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
     <property name="connection.url">jdbc:derby://localhost:1527/K:\db</property> 
     <property name="connection.username"></property> 
     <property name="connection.password"></property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.DerbyDialect</property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">update</property> 
    </session-factory> 
</hibernate-configuration> 

Co mogę zrobić, aby wyeliminować ten wyjątek?

Odpowiedz

13

Dodaj linię

<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 

jako drugiej linii (po wersji xml i przed znacznikiem <hibernate-configuration>. Wtedy powinno działać.

+5

Dodanie 'DOCTYPE' nie jest wystarczające, musiałem również usunąć wszystkie atrybuty z elementu' hibernacji-konfiguracji' Znajdź dziwne, że hibernacja faworyzuje 'DTD' ponad' XSD'. możesz również zaktualizować adres URL 'DTD', aby użyć' 'http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" 'zamiast starego adresu URL sourceforge. – Sydney

1

Usuń ten kod i problem zostanie resloved.

xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
Powiązane problemy