我是hibernate的新手。当我实例化会话工厂时,我收到错误
信息:HHH000206:找不到hibernate.properties
但是hibernate.cfg.xml位于src的根目录。
这是我如何尝试实例化它:
SessionFactory hbrntFctry;
Session rslt = null;
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().configure().buildServiceRegistry();
try
{
hbrntFctry = new Configuration().configure().buildSessionFactory(serviceRegistry);
rslt = hbrntFctry.openSession();
}
catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
然后它说:
线程“main”中的异常org.hibernate.internal.util.config.ConfigurationException:无法在RESOURCE hibernate.cfg.xml中的第4行和第26行执行解组。消息:cvc -elt.1:找不到元素'hibernate-configuration'的声明。 在org.hibernate.service.internal.JaxbProcessor.unmarshal(JaxbProcessor.java:120) 在org.hibernate.service.internal.JaxbProcessor.unmarshal(JaxbProcessor.java:69) 在org.hibernate.service.ServiceRegistryBuilder.configure(ServiceRegistryBuilder.java:162) 在org.hibernate.service.ServiceRegistryBuilder.configure(ServiceRegistryBuilder.java:147) 在com.foampile.collect.UsgsRetriever.openHibernateSession(UsgsRetriever.java:250) 在com.foampile.collect.UsgsRetriever.getStateGauges(UsgsRetriever.java:78) 在com.foampile.base.MainApp.main(MainApp.java:22) 引起:javax.xml.bind.UnmarshalException - 链接异常: [org.xml.sax.SAXParseException; lineNumber:4; columnNumber:26; cvc-elt.1:找不到元素'hibernate-configuration'的声明。] at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:419) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:356) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:326) 在org.hibernate.service.internal.JaxbProcessor.unmarshal(JaxbProcessor.java:108) ......还有6个 引起:org.xml.sax.SAXParseException; lineNumber:4; columnNumber:26; cvc-elt.1:找不到元素'hibernate-configuration'的声明。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) 在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.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1897) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:737) at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:564) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:86) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:232) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:166) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:354) ......还有8个
这是我的cfg文件:
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">jdbc:mysql://localhost/foampile</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- List of XML mapping files -->
<mapping resource="SiteRecord.hbm.xml"/>
</session-factory>
答案 0 :(得分:4)
您的hibernate.cfg.xml文件中存在错误。 </hibernate-configuration>
遗失了。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
...
</session-factory>
</hibernate-configuration>