我正在尝试更改!DOCTYPE但仍无法正常工作
My Test.java
import com.serwis.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
* Created by Jodanpotasu on 2016-07-17.
*/
public class TEST {
public static void main(String[] args){
Configuration configuration = new Configuration().configure(); //with configure("hibernate.cfx.xml still not working
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
try{
User user = new User(1,"login","password","mail",null);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
}catch (Exception e){
System.out.println("blad");
} finally {
System.out.println("zrobiono");
}
}
}
我的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
<session-factory>
<!-- JDBC connection settings -->
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:9001</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!--https://www.progress.com/jdbc/resources/tutorials/connection-pooling/connection -->
<property name="connection.pool_size">1</property>
<!-- SQL DIALECT -->
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- SHOW SQL OUTPUT -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
和控制台的输出
线程中的异常&#34; main&#34; org.hibernate.MappingException:无效 配置在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160) 在org.hibernate.cfg.Configuration.configure(Configuration.java:2077) 在org.hibernate.cfg.Configuration.configure(Configuration.java:2056) 在com.serwis.TEST.main(TEST.java:17)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 引起:org.xml.sax.SAXParseException; lineNumber:6;得到columnnumber: 87;属性&#34; xmlns&#34;必须为元素类型声明 &#34;冬眠配置&#34 ;.在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284) 在 com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1253) 在 com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1917) 在 com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:742) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:614) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3135) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:880) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) 在 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) 在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) 在 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643) 在org.dom4j.io.SAXReader.read(SAXReader.java:465)at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157) ......还有8个
使用退出代码1完成处理
我在哪里做错了?
答案 0 :(得分:1)
从
中删除命名空间元素(xmlns) <hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration">