Hibernate org.hibernate.MappingException:配置无效

时间:2012-08-31 18:33:01

标签: java hibernate orm

我开始学习hibernate。但是当我尝试使用hibernate.cfg.xml.时,我在控制台上看到了:

Exception in thread "main" org.hibernate.MappingException: invalid configuration
    at 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)
    at testHibernate.HibernateStart.main(HibernateStart.java:28)
Caused by: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 49; Attribute "class" must be declared for element type "mapping".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:763)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:353)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2011)
    ... 3 more 

hibernate.cfg.xml文件。

> <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE
> hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration
> DTD 2.0//EN"
> "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> 
> 
> <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.EmbeddedDriver</property>
>     <property name="connection.url">jdbc:derby:/home/user/DataBase/db;create=true</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>
>     <mapping class="testHibernate.UserDetails"/>   </session-factory> </hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,文件看起来像:

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

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
     <property name="connection.url">jdbc:derby:/home/user/DataBase/db;create=true</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>

    <!-- 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">create</property>

    <mapping class="testHibernate.UserDetails"/>

</session-factory>