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. RedHat 6.7 Enterprise x64环境下使用RHCS部署Oracle 11g R2双机双实例HA

     环境 软硬件环境 硬件环境: 浪潮英信服务器NF570M3两台,华为OceanStor 18500存储一台,以太网交换机两台,光纤交换机两台. 软件环境: 操作系统:Redhat Enterpris ...

  2. python ide ---wing 注册机

    注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...

  3. JAVA学习笔记(二):eclipse智能提示(转)

    存盘 Ctrl+s(肯定知道)注释代码 Ctrl+/取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了)代码辅助 Alt+/快速修复 Ctrl+1代码格式化 Ctrl+Shift+f整 ...

  4. Python:线程

    Python中创建线程有两种方式:函数或者用类来创建线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程. 类:创建threading.Thread的 ...

  5. sqlserver索引与查询优化

    此文为转载,仅做保存使用,出处:http://www.cr173.com/html/8688_all.html 在数据库存优化设计中往往会提到索引,这编文章就来详细的说明一下在 SQL SERVER ...

  6. (一)startup.bat

    那么startup.bat(// TODO startup.sh暂缓)启动后发生了什么呢? @echo off rem Licensed to the Apache Software Foundati ...

  7. 用clock()函数计算多项式的运行时间

    百度百科中定义clock():clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock(void) ; 简 ...

  8. jvm、jre、jdk

    JVM和JRE浅谈 Java运行时环境JRE包含:Java虚拟机.库函数.运行Java应用程序和Applet所必须的文件,JDK(又称SDK)包含:JRE的超集.编译器.调试器等开发工具. java平 ...

  9. 【python】将一个正整数分解质因数

    def reduceNum(n): '''题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5''' print '{} = '.format(n), : print 'Pleas ...

  10. DBCC SHOWCONTIG、DBCC DBREINDEX。

    use dbname declare @table_id int set @table_id=object_id('tablename') dbcc showcontig(@table_id) 解释如 ...