首页 > 代码库 > hibernate操作步骤(代码部分)

hibernate操作步骤(代码部分)

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     @Test
18     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操作步骤(代码部分)