hibernate.cfg.xml文件配置中:

<property name="hibernate.hbm2ddl.auto">update</property>
update首先查询数据库的表,如果有表不会删除,
直接追加,前提,表和我对象是映射符合要求
不符合直接报错,
<property name="hibernate.hbm2ddl.auto">create</property>
这种方式呢每次都会把映射文件对应的表删除,
创建新的表,一般执行一次;

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 数据库的四要素 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">victor</property>
<property name="hibernate.connection.password">victor</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.show_sql">true</property>
<property name="format SQL in log and console hibernate.format_sql">true</property>
<mapping resource="com/****/Bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

package com.briup.basic;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.briup.Bean.Person;
public class HibernateTest {
private SessionFactory sf;
private Session session;
private Transaction tran;
@Before
public void before(){
Configuration con=new Configuration().configure();
sf=con.buildSessionFactory();
session=sf.openSession();
tran=session.beginTransaction();
}
@After
public void after(){
//把SQL语句刷到数据库,执行的是单纯的SQL语句
//把对象刷到数据库中,调用对象的get方法
//给?占位符赋值
tran.commit();
//关闭session【因为session不安全】
//也会提交事务
session.close();
}
@org.junit.Test
public void savePerson(){
Person p=new Person();
p.setName("lisi");
p.setGender("女");
p.setBird(new Date());
//根据传入的参数和方法名获取相应的SQL语句
//把传入的对象保存到缓存中
session.save(p);
}
@org.junit.Test
public void Cache(){
Person p=new Person();
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
session.save(p);
//对象已经写到缓存中,调用insert语句
//操作同一个对象p,调用update语句
//p.setName("wangwu");
tran.commit();
//可以拿到缓存中的对象但是不能操作属性
//p.setName("wangwu");
//获取缓存中的对象
//【事务的提交不影响获取缓存中的数据】
//获取内容的时候可以事务已经提交
//通常情况下习惯性做法是select直接写在事务中
Person per=(Person) session.get(getClass(), 1L);
System.out.println(per);
}
@org.junit.Test
public void saveOrUpdate(){
try {
Person p=new Person();
p.setId(1L);
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
//先查看你的主键在数据库中是否存在
//如果不存在 直接执行select-------save
//存在 执行select-------update
p.setId(1L);
p.setName("wangwu");
p.setGender("男");
p.setBird(new Date());
//缓存中不能粗线两个对象对应着的同一主键
//在事务没有提交之前,最好自己设置不同的主键
//每次都是调用select max(id) from table;
//如果没有主动设置主键的没主键全是一样的,前提是看主键自动生成策略是什么【increment】
session.saveOrUpdate(p);
tran.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@org.junit.Test
public void saveOrUpdate1(){
try {
Person p=new Person();
p.setId(1L);
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
//merge和saveOrUpdate类似,****【区别】****
session.merge(p);
tran.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
//merge和saveOrUpdate类似,****【区别】****
//merge这种保存方式会将两个缓存中一样的【通过主键】
//相同的数据,不同的对象,按照先insert后update
@org.junit.Test
public void saveOrUpdate2(){
try {
Person p=new Person();
//如果没有主键的情况下
//p.setId(1L);
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
session.merge(p);
//先查看你的主键在数据库中是否存在
//如果不存在 直接执行select-------save
//存在 执行select-------update
//p.setId(1L);
//不加id就是普通的insert语句
p.setName("wangwu");
p.setGender("男");
p.setBird(new Date());
//缓存中不能粗线两个对象对应着的同一主键
//在事务没有提交之前,最好自己设置不同的主键
//每次都是调用select max(id) from table;
//如果没有主动设置主键的没主键全是一样的,前提是看主键自动生成策略是什么【increment】
session.merge(p);
tran.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@org.junit.Test
public void flush(){
try {
//flush把缓存中的数据sql语句强制刷到数据库中执行
Person p=new Person();
p.setId(1L);
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
//merge和saveOrUpdate类似,****【区别】****
session.save(p);
session.flush();//不会刷到数据库里
tran.commit();
session.clear();//清空缓存中的对象
Person per=(Person) session.get(getClass(), 1L);
System.out.println(per);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@org.junit.Test
public void clear(){
try {
//清空缓存中的对象
Person p=new Person();
p.setId(1L);
p.setName("lisi");
p.setGender("男");
p.setBird(new Date());
//merge和saveOrUpdate类似,****【区别】****
session.save(p);
tran.commit();
session.clear();
Person per=(Person) session.get(getClass(), 1L);
System.out.println(per);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@org.junit.Test
public void session(){
//openSession()每次创建新的session,不关之前是否有session实现的接口
Session session1=sf.openSession();
Session session2=sf.openSession();
Session session3=sf.openSession();
System.out.println(session1==session2);
System.out.println(session2==session3);
System.out.println(session1==session3);
}
@org.junit.Test
public void getCurrentSession(){
//获取session
try {
//Session session=sf.openSession();和Session session1=sf.getCurrentSession();开启的session和获取的session不是同一个session
//因为在获取getCurrentSession()时会创建一个新的session
//Session session=sf.openSession();
Session session1=sf.getCurrentSession();
Session session2=sf.getCurrentSession();
Session session3=sf.getCurrentSession();
System.out.println(session1==session2);
System.out.println(session2==session3);
System.out.println(session1==session3);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

}

@Test
public void AutoCreateTable() {
Configuration con=new Configuration().configure("/hibernate.cfg.xml");
SchemaExport create=new SchemaExport(con);
create.create(false, true);
}
@org.junit.Test
public void UpdateCreateTable(){
try {
Configuration con=new Configuration().configure();
SessionFactory sf=con.buildSessionFactory();
Session session=sf.openSession();
Transaction tran=session.beginTransaction();
Person person=new Person();
person.setName("zhouzhiwei");
person.setGender("男");
person.setBird(new Date());
session.save(person);
tran.commit();
session.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

Configuration conf=new Configuration().configure("/hibernate.cfg.xml");
SchemaExport create=new SchemaExport(conf);
//第一个参数代表是否控制台显示sql,false是因为
//hibernate.properties这个文件已经设置;
create.create(false, true);
单纯的建表,先删除在建;

Hibernate学习笔记2的更多相关文章

  1. Hibernate学习笔记(二)

    2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...

  2. Hibernate学习笔记(一)

    2016/4/18 19:58:58 Hibernate学习笔记(一) 1.Hibernate框架的概述: 就是一个持久层的ORM框架. ORM:对象关系映射.将Java中实体对象与关系型数据库中表建 ...

  3. Hibernate 学习笔记一

    Hibernate 学习笔记一 今天学习了hibernate的一点入门知识,主要是配置domain对象和表的关系映射,hibernate的一些常用的配置,以及对应的一个向数据库插入数据的小例子.期间碰 ...

  4. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

  5. Hibernate学习笔记

    一.Hibernate基础 1.Hibernate简介 Hibernate是一种对象关系映射(ORM)框架,是实现持久化存储的一种解决方案.Java包括Java类到数据库表的映射和数据查询及获取的方法 ...

  6. Hibernate学习笔记(四)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...

  7. Hibernate学习笔记(三)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...

  8. HIbernate学习笔记(一) 了解hibernate并搭建环境建立第一个hello world程序

    Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了轻量级的封装,Java程序员可以使用面向对象的编程思维来操纵数据库,它通过对象属性和数据库表字段之间的映射关系,将对象 ...

  9. Hibernate学习笔记-Hibernate关系映射

    1. 初识Hibernate——关系映射 http://blog.csdn.net/laner0515/article/details/12905711 2. Hibernate 笔记8 关系映射1( ...

  10. Hibernate学习笔记(1)Hibernate构造

    一 准备工作 首先,我们将创建一个简单的基于控制台(console-based)Hibernate应用. 我们所做的第一件事就是创建我们的开发文件夹.并把所有需要用到的Java件放进去.解压缩从Hib ...

随机推荐

  1. Linux下修改默认字符集--->解决Linux下Java程序种中文文件夹file.isDirectory()判断失败的问题

    一.问题描述: 一个项目中为了生成树状目录,调用了file.listFiles()方法,然后利用file.isDirectory()方法判断是否为目录,该程序在windows下运行无问题,在Linux ...

  2. php几个常用的概率算法(抽奖、广告首选)

    做网站类的有时会弄个活动什么的,来让用户参加,既吸引用户注册,又提高网站的用户活跃度.同时参加的用户会获得一定的奖品,有100%中奖的,也有按一定概率中奖的,大的比如中个ipad.iphone5,小的 ...

  3. hdu 4035 2011成都赛区网络赛E 概率dp ****

    太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...

  4. PING命令入门详解

    转自:http://www.linkwan.com/gb/tech/htm/928.htm 1.Ping的基础知识 ping命令相信大家已经再熟悉不过了,但是能把ping的功能发挥到最大的人却并不是很 ...

  5. win8.1/win10 UEFI + GPT 安装(测试机型:华硕S56CM)

    本教程简要介绍在UEFI 启动模式下在GPT分区表中,最简单的方法安装 Windows 10 x64 位系统.(并非傻瓜教程,安装者总要有一定的经验基础)下面先简单介绍一下UEFI和GTP. UEFI ...

  6. Java Server returned HTTP response code: 401

    今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...

  7. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  8. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  9. 函数式编程语言LISP,python,haskell,clojure

    说说我自己的背景吧,我是个半吊子的程序员,做任何事情喜欢比较了解然后再尝试,我接触过很多语言,大多数都把它当成工具来使用 我现在的工作大部分主要在于数据挖掘与机器学习方面,也学习web开发,我第一个拿 ...

  10. ios摇一摇截屏代码

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...