2

第一次使用 Hibernate 开箱即用,并得到一个奇怪的错误。我正在使用该4.2.3 Final版本,并希望使用4.0 XSD 架构,但如果需要它工作,我将使用 3.0 DTD。

我的司机:

public static void main(String[] args) {
    Configuration configuration = new Configuration();
    configuration.configure("hibernate/cfg/hibernate.cfg.xml");
    ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration
            .getProperties());
    SessionFactory sessionFactory = configuration
            .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
    Session session = sessionFactory.openSession();

    Transaction tx = session.beginTransaction();

    User user = new User("Dr.", "Sylvester", "McMonkey", "McBean", "IV");

    session.save(user);
    tx.commit();
    session.close();
}

我的hibernate.cfg.xml文件,位于我的项目中src/main/config/hibernate/cfg

<?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>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://my-server:3306/my_db</property>
        <property name="hibernate.connection.username">myuser</property>
        <property name="hibernate.connection.password">password</property>

        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <property name="hbm2ddl.auto">validate</property>

        <mapping resource="User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

stacktrace Eclipse 给了我:

Jul 27, 2013 9:10:08 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jul 27, 2013 9:10:08 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.3.Final}
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate/cfg/hibernate.cfg.xml
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate/cfg/hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2070)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
    at com.apponomical.adaptors.datasource.HibernateDriver.main(HibernateDriver.java:15)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found.
    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:387)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:321)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:226)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3080)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:899)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:625)
    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:819)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
    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:1208)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
    ... 2 more

想法?想法?建议?提前致谢!

4

2 回答 2

1

验证器未找到架构。xsi:schemaLocation 属性告诉它在与您正在验证的配置文档 (.../src/main/config/hibernate/cfg) 相同的目录中查找模式文档 hibernate-configuration-4.0.xsd。那是你有它的地方吗?

于 2013-07-27T22:08:47.617 回答
0

在 Hibernate 4.x 中,您应该使用与 3.x 相同的 DTD,正如您在此错误报告中看到的那样,其中删除了弃用警告。4.2 版本的教程也使用这个,你可以在这里看到。

于 2014-01-09T12:03:47.410 回答