Use Hibernate core API
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:
@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());
}
@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的更多相关文章
- AspNet Core Api Restful 实现微服务之旅 (一)
(一)了解微服务(二)搭建VS项目框架 (三)创建AspNet Core Api VS2017 安装包 链接:https://pan.baidu.com/s/1hsjGuJq 密码:ug59 创 ...
- 【从零开始搭建自己的.NET Core Api框架】(七)授权认证进阶篇
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- .net core api +swagger(一个简单的入门demo 使用codefirst+mysql)
前言: 自从.net core问世之后,就一直想了解.但是由于比较懒惰只是断断续续了解一点.近段时间工作不是太忙碌,所以偷闲写下自己学习过程.慢慢了解.net core 等这些基础方面学会之后再用.n ...
- 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 详解ASP.NET Core API 的Get和Post请求使用方式
上一篇文章帮助大家解决问题不彻底导致博友使用的时候还是遇到一些问题,欢迎一起讨论.所以下面重点详细讲解我们常用的Get和Post请求( 以.net core2.2的Http[Verb]为方向 ,推荐该 ...
- ASP.NET Core API 接收参数去掉烦人的 [FromBody]
在测试ASP.NET Core API 项目的时候,发现后台接口参数为类型对象,对于PostMan和Ajax的Post方法传Json数据都获取不到相应的值,后来在类型参数前面加了一个[FromBody ...
- Ubuntu server 运行.net core api 心得
1.安装.net core sdk 在微软.net core 安装页面找到linux 安装,按照步骤安装好 2.安装mysql 参考 Ubuntu安装mysql 3.配置mysql 1.需要将mysq ...
- ASP.NET CORE API Swagger+IdentityServer4授权验证
简介 本来不想写这篇博文,但在网上找到的文章博客都没有完整配置信息,所以这里记录下. 不了解IdentityServer4的可以看看我之前写的入门博文 Swagger 官方演示地址 源码地址 配置Id ...
随机推荐
- [html]LESS-1.3.3
网站:http://www.bootcss.com/p/lesscss/ 下载链接:http://files.cnblogs.com/files/z5337/less-1.3.3.min.js
- 03 Apache Solr: 安装和运行
前面介绍了Solr在项目中的使用和构建高度可用.高度可扩展的Solr服务器的一些想法.但是光说不练假把式,现在开始,把Solr运行起来继续深入了解吧! 安装 安装JAVA Apache So ...
- JS编写背景图切换
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- DBlink与同义词
DBLink就是数据库链接,而同义词就已经具体到某个用户下的表了 原文链接:http://www.linuxidc.com/Linux/2013-01/77579.htm 这里所需要的信息: 从MM库 ...
- 基类用的this指针
结论:基类构造函数中的this指针指向的是派生类的对象 测试代码: #include <iostream> using namespace std; class father; fathe ...
- 通过ssh连接github
1.检查是否已经存在ssh key $ cd ~/.ssh $ ls 如果该目录下存在id_rsa/id_rsa.pub/known_hosts这三个文件,则已经存在ssh key 直接跳转到第3步 ...
- Forwards on Weibo (30)
BFS,题意比较难懂,是求离query L层的总共人数 #include <stdio.h> #include <string.h> #include <iostream ...
- C# Form.Close 的释放问题
今天使用From窗口Close后,发现From的资源还存在,并没有释放资源,只有在程序关闭的时候才去释放. Form1:button按钮 private void button1_Click(obje ...
- 通过通知监听键盘的状态来改变View的位置
#import "ViewController.h" @interface ViewController ()<UITextFieldDelegate>{ UIV ...
- 百度地图开发 Android版应用Key申请
一 申请API key 在使用百度地图之前,我们必须去申请一个百度地图的API key,申请地址http://lbsyun.baidu.com/apiconsole/key,自己自行注册一个百度账号, ...