For Hibernate configuration, We can use hibernate.cfg.xml file to configure:

 <?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> <session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
You configure Hibernate's SessionFactory, SessionFactory is a glob factory responsible for a particular database.
If you have several databases, for easier startup you shuould use several <session-factory> configurations in your several configration files.
(in java code use configure("xml file name") to chose configuration which is use for.)
the first four property elements is used for configure jdbc connection.
<property name="current_session_context_class">thread</property>
In Hibernate 4.x use currentSession. So This element provide the context class.
the value thread is use for tomcat and others which server is not suport several databases.
The value can be also "jta",Jta can be used for server which is suport several databases like JBOSS.
<mapping class="com.hibernate.model.Teacher"/> This element mapping to The java calss you created. 
The class must use annotation like tihs:
 @Entity
@Table(name="_teacher")
//If Object name is not same as table's name, use this annotate
public class Teacher {
@Id
@GeneratedValue
//if you want to automate id. Just use @GeneratedValue
//by default all database can automate id
//When use Database which suport identity. just use @GeneratedValue(strategy = GenerationType.IDENTITY)
//When use database which suport sequence. just use @GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} @Column(name="_name")
//If Object name is not same as column use this annotate
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Transient
//When you don't need to save this object to DB, just use @Transient
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
} @Temporal(TemporalType.DATE)
//just save date and type integer database is Date
public Date getBrithDate() {
return brithDate;
}
public void setBrithDate(Date brithDate) {
this.brithDate = brithDate;
}
@Enumerated(EnumType.STRING)
//use STRING will save a string in database witch you wrote in enumerate;
//use ORDINAL will save a integer type in database order by enumerate elements;
public Zhicheng getZhicheng() {
return zhicheng;
}
public void setZhicheng(Zhicheng zhicheng) {
this.zhicheng = zhicheng;
} private int id;
private String name;
private String title;
private String yourWifeName;
private Date brithDate;
private Zhicheng zhicheng; }
the annotation @Entity means this class can mapping for confguration.
@Id before Getter method, that means this proprety is the primary key for table in database.

If you want save data to database. Just do like this:
At the first, you must build the SessionFactory. The SessionFactory build by configuration file (hibernat.cfg.xml). So code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Then, you should get Session from SessionFactory. Because SessionFactory comes from the configration.
Session session = getSessionFactory().getCurrentSession();
Use getCurrentSession() method to get session. Maybe, The Session is created aready. Because getSessionFactory() get the session from context.

You can use this session until you commit it.
session.beginTransaction();
When you need to transact data, Just beginTransaction. Then you can do something for database.
session.save(d);
session.getTransaction().commit();
When you commit the session, CurrentSession will be destoryed.

The datas' there stats:
  When you create object, It where be transient, In memory only has a object no id in map.
  When you save object to databases, It wiil be prisistent, In memory has Id in map (memory will create map to mapping object by id) and It is also be saved in database.
  When commit the Transactiong, It will be deteched,In memory no object, no id, But it's saved database already.
Select data from databases:
We can use Session.get() and Session.load()
When use get() method:
    The get() method has two parameter: get(Object, Serializable);
    First parameter is an Object which is used for mapping table.
    Second parameter is an Serialzable object which is used for for primary key.
Use get() method, program will send SQL string to database speedy and all data you got will be set to object.
So, Even though the Session is be comiited. You can also get data from your object.
like this:
 @Test
public void testGetProgramer() {
ProgramerPK pp = new ProgramerPK();
pp.setId(1);
pp.setSid("123");
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Programer p = (Programer)session.get(Programer.class, pp);
session.getTransaction().commit();
System.out.println(p.getName());
}
When use load() method:
 
    The load() method has two parameter: get(Object, Serializable);
 
    First parameter is an Object which is used for mapping table.
 
    Second parameter is an Serialzable object which is used for for primary key.
 
Use load() method, program will send SQL string to database when you use Object's getter method.
 
So, If the Session is be commited. You cannot get data by Objects.getXXX().
 
It means you must get the data before you commit Session. Because The Object you got from load() it not yours. 
 
It's Just a Proxy.
 
Use is like this:
 
  @Test
public void testLoadProgramer() {
ProgramerPK pp = new ProgramerPK();
pp.setId(1);
pp.setSid("123");
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Programer p = (Programer)session.load(Programer.class, pp);
System.out.println(p.getName());
session.getTransaction().commit();
}

Use Hibernate core API的更多相关文章

  1. AspNet Core Api Restful 实现微服务之旅 (一)

    (一)了解微服务(二)搭建VS项目框架  (三)创建AspNet Core Api VS2017 安装包   链接:https://pan.baidu.com/s/1hsjGuJq 密码:ug59 创 ...

  2. 【从零开始搭建自己的.NET Core Api框架】(七)授权认证进阶篇

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  3. .net core api +swagger(一个简单的入门demo 使用codefirst+mysql)

    前言: 自从.net core问世之后,就一直想了解.但是由于比较懒惰只是断断续续了解一点.近段时间工作不是太忙碌,所以偷闲写下自己学习过程.慢慢了解.net core 等这些基础方面学会之后再用.n ...

  4. 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  5. 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  6. 详解ASP.NET Core API 的Get和Post请求使用方式

    上一篇文章帮助大家解决问题不彻底导致博友使用的时候还是遇到一些问题,欢迎一起讨论.所以下面重点详细讲解我们常用的Get和Post请求( 以.net core2.2的Http[Verb]为方向 ,推荐该 ...

  7. ASP.NET Core API 接收参数去掉烦人的 [FromBody]

    在测试ASP.NET Core API 项目的时候,发现后台接口参数为类型对象,对于PostMan和Ajax的Post方法传Json数据都获取不到相应的值,后来在类型参数前面加了一个[FromBody ...

  8. Ubuntu server 运行.net core api 心得

    1.安装.net core sdk 在微软.net core 安装页面找到linux 安装,按照步骤安装好 2.安装mysql 参考 Ubuntu安装mysql 3.配置mysql 1.需要将mysq ...

  9. ASP.NET CORE API Swagger+IdentityServer4授权验证

    简介 本来不想写这篇博文,但在网上找到的文章博客都没有完整配置信息,所以这里记录下. 不了解IdentityServer4的可以看看我之前写的入门博文 Swagger 官方演示地址 源码地址 配置Id ...

随机推荐

  1. 在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib

    为什么要CMake,这里我陈述自己的想法,作为一个刚使用opencv库的小白来说,有以下大概三点内容 1.由于在学习图像处理滤波器中,需要用到各种边缘保护滤波器(EPS)算法,但是这些算法在OpenC ...

  2. python实践——批量统计mongodb数据库的集合大小

    #!/usr/bin/env python import os,sys list = [] for i in range(3,50): l = os.popen("/bin/echo 'sh ...

  3. UILabel添加图片之富文本的简单应用

    若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...

  4. MVC 前台向后台传输数据

    今天,我们一起来学习下.MVC如何在前台给后台传输数据 (1)前台传输数据到后台 具体思路:前台拼凑json字符串,然后通过 get 或 post 方式,传递到后台 Action 方法中 我现在前台展 ...

  5. SEO和SEM的区别

    SEO是属于SEM的一部分,SEO和SEM最主要的是最终目标的不同: SEO主要是为了关键词的排名.网站的流量.网站的结构.搜索引擎中页面收录的数据. SEM是通过SEO技术基础上扩展为搜索引擎中所带 ...

  6. filter之排除个别过滤

    1.jsp 篇 一般拦截器设置都是拦截*.action.*.jsp等,如此我们可以扩展后缀名,逃过拦截: jsp的话,可以改成.jspf后缀. ( 把一个JSP文件命名为jspf扩展名,然后inclu ...

  7. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  8. flex柱状图

    柱状图的展现是通过flex自带的控件实现 控件解析:<mx:ColumnChart id = "columns" dataProvider = "{dataSour ...

  9. iOS—如何申请苹果公司开发者账号流程详细图文介绍(包括邓白氏编码的申请方法详细介绍)

    我们要申请开发者账号,首先就需要先注册一个苹果的apple id,然后再这个账号的基础上去继续,这个相信大家都知道 这是申请appleid的地址:https://appleid.apple.com/a ...

  10. 纯脚本组装Json格式字符串

    var answerStr = "["; for (var i in answer) { var data = $("input[name=QuestionItem_&q ...