Hibernate编译问题

时间:2015-01-04 16:35:36

标签: java hibernate

我必须使用Hibernate来制作我的项目。问题是我无法找到解决问题的方法。我无法编译我的程序,因为我遇到了一些问题(我在输出控制台下面复制了)

hibernate.cfg.xml中:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="connection.username">sa</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.HSQLDialect</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.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 resource="events/Event.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

我的主要课很简单:

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Event theEvent = new Event();
        theEvent.settitle(title);
        theEvent.setdate(theDate);

        session.save(theEvent);

        session.getTransaction().commit();

当我尝试编译我的程序时,我得到的是这样的东西:

Jan 04, 2015 5:29:32 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jan 04, 2015 5:29:32 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jan 04, 2015 5:29:32 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 04, 2015 5:29:32 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 04, 2015 5:29:32 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 04, 2015 5:29:32 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 04, 2015 5:29:32 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: events/Event.hbm.xml
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : Element type "hibernate-mapping" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (3) : Element type "class" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (4) : Element type "id" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (5) : Element type "generator" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (7) : Attribute "type" must be declared for element type "property".
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (7) : Attribute "column" must be declared for element type "property".
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : Element type "hibernate-mapping" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (3) : Element type "class" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (4) : Element type "id" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (5) : Element type "generator" must be declared.
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (7) : Attribute "type" must be declared for element type "property".
Jan 04, 2015 5:29:32 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (7) : Attribute "column" must be declared for element type "property".
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.ExceptionInInitializerError
    at HibernateUtil.<clinit>(HibernateUtil.java:22)
    at EventManager.listEvents(EventManager.java:45)
    at EventManager.main(EventManager.java:18)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
    at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:375)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:304)
    at org.hibernate.cfg.Configuration.add(Configuration.java:516)
    at org.hibernate.cfg.Configuration.add(Configuration.java:512)
    at org.hibernate.cfg.Configuration.add(Configuration.java:686)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at HibernateUtil.<clinit>(HibernateUtil.java:18)
    ... 2 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 20; Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    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.rootElementSpecified(XMLDTDValidator.java:1599)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1877)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:742)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:325)
    ... 14 more
Java Result: 1

Event.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
    <class name="events.Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>
</hibernate-mapping>

我该如何解决这个问题?

提前致谢!

编辑: 嗨! 我再次更改了meskobalazs(在hibernate.cfg.xml中)提到的内容。 该程序继续运行,但仍然遇到一些错误和错误:

Jan 04, 2015 6:05:46 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jan 04, 2015 6:05:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: events/Event.hbm.xml
Jan 04, 2015 6:05:46 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.hsqldb.jdbcDriver] at URL [jdbc:hsqldb:hsql://localhost]
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa, password=****}
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Jan 04, 2015 6:05:47 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
Jan 04, 2015 6:05:47 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
Exception in thread "main" java.lang.ExceptionInInitializerError
    at HibernateUtil.<clinit>(HibernateUtil.java:22)
    at EventManager.listEvents(EventManager.java:45)
    at EventManager.main(EventManager.java:18)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
    at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:100)
    at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:252)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1386)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at HibernateUtil.<clinit>(HibernateUtil.java:18)
    ... 2 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

2 个答案:

答案 0 :(得分:2)

更改此

<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-mapping>

<!DOCTYPE hibernate-mapping SYSTEM
    "classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

您引用了错误的DTD。

答案 1 :(得分:0)

更改来自

的Event.hbm.xml实体映射文件头
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

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