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. maven 依赖查询

    该文章源地址:http://xiejianglei163.blog.163.com/blog/static/1247276201362733217604/ 为方便个人使用,转载于此处. http:// ...

  2. Oracle 创建/删除 表空间、用户、授权

    首先以DBA连接到数据库:sqlplus / as sysdba; --创建表空间 create tablespace test_tablespace datafile 'D:\developer\o ...

  3. html5 三角形

    html5 三角形 <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...

  4. h264 profile & level

    转自:http://blog.csdn.net/sphone89/article/details/17492433 H.264 Profiles H.264有四种profile,每个profile支持 ...

  5. HR外包系统 - 工资计算-几种常见账单计算规则

    01-正常工资计税 (包括同一月多地计税方式) 02-年终奖计税 (包括可分批发放,但计税总额不变)  按工资 除以月份,看落在那个计税区间,获取税率和扣除数,再用总额*税率-扣除数,要考虑当月工资如 ...

  6. Computer Graphics Research Software

    Computer Graphics Research Software Helping you avoid re-inventing the wheel since 2009! Last update ...

  7. Codeforces Round #370 (Div. 2) D. Memory and Scores DP

    D. Memory and Scores   Memory and his friend Lexa are competing to get higher score in one popular c ...

  8. 如何安装Ecshop for linux

    下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...

  9. 去除android手机滚动条

    方法1:::-webkit-scrollbar{display: none;} 方法2:::-webkit-scrollbar{height:0; width:0:}

  10. POJ 3974 回文串-Manacher

    题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...