1. Load Hibernate's core configuration file
2. Create a Sessionfactory object
3. Create a Session object using Sessionfactory
4. Turn on the transaction (open manually)
5. Write specific logical crud, delete and change the operation
6. Submit Things
7. Close Resources
The code is as follows:
1 Packagecom.hui.hibernate;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.Transaction;6 Importorg.hibernate.cfg.Configuration;7 Importorg.junit.Test;8 9 Public classHibernateDemo01 {Ten One Public Static voidMain (string[] args) { A Add (); - - } the -@SuppressWarnings ("Deprecation") - @Test - Public Static voidAdd () { + //1. Load Hibernate's core configuration file -Configuration con =NewConfiguration ();//load the configuration file object, locate the Hibernate.cfg.xml file under SRC and place it in the object +Con.configure ();//Start loading A // at //2. Create a Sessionfactory object, read the core profile content creation sessionfactory, create a table based on the mapping relationship -Sessionfactory SF =con.buildsessionfactory (); - //3. Create a Session object using Sessionfactory - //similar to Connection -Session SE =sf.opensession (); - //4. Turn on the transaction (open manually) inTransaction TR =se.begintransaction (); - //5. Write specific logical crud, delete and change operations, manipulate entity class objects toUser U =NewUser ();//UID does not set the value, he is the self-growing primary key +U.setusername ("Liu Dongdong"); -U.setpassword ("123456"); theU.setaddress ("Shaanxi, China"); * //call the session method save to complete the operation $ se.save (u);Panax Notoginseng - //6. Submit Things the tr.commit (); + //7. Close Resources A se.close (); the sf.close (); + - } $ $}
8. Test, run Error
The following errors are reported
Log4j:warn No Appenders could is found for logger (org.jboss.logging). Log4j:warn Please initialize the log4j system proper Ly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" 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:518) at Org.hibernate.cfg.Configuration.add ( configuration.java:514) at Org.hibernate.cfg.Configuration.add (configuration.java:688) at Org.hibernate.cfg.Configuration.addResource (configuration.java:771) at Org.hibernate.cfg.Configuration.parseMappingElement (configuration.java:2257) at Org.hibernate.cfg.Configuration.parseSessionFactory (configuration.java:2229) at Org.hibernate.cfg.Configuration.doConfigure (configuration.java:22(configuration.java:2162) at Org.hibernate.cfg.Configuration.doConfigure Org.hibernate.cfg.Configuration.configure (configuration.java:2077) at Org.hibernate.cfg.Configuration.configure ( configuration.java:2056) at Com.hui.hibernate.HibernateDemo01.add (hibernatedemo01.java:21) at Com.hui.hibernate.HibernateDemo01.main (hibernatedemo01.java:12) caused by:org.xml.sax.SAXParseException; linenumber:22; columnnumber:40; The attribute "Colnum" must be declared for the element type "id". 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:396) at Com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError (xmlerrorreporter.java:327) at Com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError (xmlerrorreporter.java:284) 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.startElement (xmldtdvalidator.java:742) at Com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement ( xmlnsdocumentscannerimpl.java:380) at com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl$ Fragmentcontentdriver.next (xmldocumentfragmentscannerimpl.java:2787) 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:118) 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:643) at Org.dom4j.io.SAXReader.read (saxreader.java:465) at Org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument (mappingreader.java:325) ... More
Reason: If the red Line part of the column is written in Colnum, so hibernate does not recognize, so the error is changed back to become
9. After the test results are as follows: Created table T_user and inserted a data
Hibernate operation steps (Code section)