hibernate操作步驟(代碼部分)

來源:互聯網
上載者:User

標籤:映射   ldo   使用   nsa   art   tar   測試   中國   ntc   

1.載入hibernate的核心設定檔

2.建立SessionFactory對象

3.使用SessionFactory建立Session對象

4.開啟事務(手動開啟)

5.寫具體邏輯crud,增刪改查操作

6.提交事物

7.關閉資源

代碼如下:

 1 package com.hui.hibernate; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.Test; 8  9 public class HibernateDemo01 {10 11     public static void main(String[] args) {12         add();13 14     }15     16     @SuppressWarnings("deprecation")17     @Test18     public static void add() {19 //        1.載入hibernate的核心設定檔20         Configuration con = new Configuration();//載入設定檔的對象,在src下找到hibernate.cfg.xml檔案放到對象中21         con.configure();//開始載入22 //23 //        2.建立SessionFactory對象,讀取核心設定檔內容建立SessionFactory,根據映射關聯建立表24         SessionFactory sf = con.buildSessionFactory();25 //        3.使用SessionFactory建立Session對象26         //類似於串連27         Session se = sf.openSession();28 //        4.開啟事務(手動開啟)29         Transaction tr = se.beginTransaction();30 //        5.寫具體邏輯crud,增刪改查操作,操作實體類對象31         User u = new User();//uid不用設定值,他為自增長的主鍵32         u.setUsername("劉冬冬");33         u.setPassword("123456");34         u.setAddress("中國陝西");35         //調用Session方法save完成操作36         se.save(u);37         38 //        6.提交事物39         tr.commit();40 //        7.關閉資源41         se.close();42         sf.close();43 44     }45 46 }

 

8.測試,運行報錯

報的錯誤如下

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.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:2209)    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162)    at 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; 必須為元素類型 "id" 聲明屬性 "colnum"。    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)    ... 13 more

原因:如紅線部分column寫成了colnum,所以hibernate不能識別了,因而報錯改回來就成了

9.改後測試結果如下:建立了表t_user而且插入了一條資料

 

hibernate操作步驟(代碼部分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.