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. 初步涉及JDBC

    一.为什么要使用JDBC: 1.在之前的学习中,我们都是通过控制台来输入信息,创建对象,然后再输出信息,但是这样无法保存数据,每次程序运行都需要重新输入,很麻烦. 2. 在这样的情况下,我们就需要利用 ...

  2. Coursera Machine Learning : Regression 评估性能

    评估性能 评估损失 1.Training Error 首先要通过数据来训练模型,选取数据中的一部分作为训练数据. 损失函数可以使用绝对值误差或者平方误差等方法来计算,这里使用平方误差的方法,即: (y ...

  3. linux shell执行中需要交互输入回车,Yes/NO Y/N

    最近写自动安装脚本遇到redis-server安装的时候,需要输入3个回车,对此尝试无果,最后google比较满意的解决办法: shell 脚本需要交互,比如输入回车,输入YES/NO Y/N之类进行 ...

  4. [z]START WITH CONNECT BY PRIOR子句实现递归查询

    [z]http://jingyan.baidu.com/article/5d368d1e182bb93f60c05784.html START WITH CONNECT BY PRIOR这个语法主要用 ...

  5. 最全的Resharper快捷键汇总

    编辑Ctrl + Space 代码完成 Ctrl + Shift + Space代码完成Ctrl + Alt + Space代码完成Ctrl + P 显示参数信息Alt + Insert 生成构造函数 ...

  6. PHP获取页面执行时间的方法

    一些循环代码,有时候要知道页面执行的时间,可以添加以下几行代码到页面头部和尾部: 头部: <?php $stime=microtime(true); 尾部: $etime=microtime(t ...

  7. 提交ajax验证用户名是否已存在

    前端页面 <tr> <td class="p_label"><span class="notnull"></span& ...

  8. SQL Server 2008 r2 中 SQL语句提示“对象名无效”,但可执行

    [问题描述]在使用 SQL Server 2008 r2 时,有时在完成SQL书写后,会提示“对象名无效”,而SQL语句可正常执行. [原因]缓存相关. [解决方法]ctrl+shift+R 刷新下, ...

  9. 深入理解php底层:php生命周期 [转]

    1.PHP的运行模式: PHP两种运行模式是WEB模式.CLI模式.无论哪种模式,PHP工作原理都是一样的,作为一种SAPI运行. 1.当我们在终端敲入php这个命令的时候,它使用的是CLI. 它就像 ...

  10. IOS 本地推送 IOS10.0以上 static的作用 const的作用

    //需要在AppDelegate里面启动APP的函数 加上 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNot ...