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. android 面试题

    一,什么是OOM (1)先从定义开始:Android(Java)中常见的容易引起内存泄漏的不良代码Android主要应用在嵌入式设备当中,而嵌入式设备由于一些众所周知的条件限制,通常都不会有很高的配置 ...

  2. PHP判断当前访问的是 微信、iphone、android 浏览器

    <?phpvar ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i)=="micromess ...

  3. 虚拟机通过NAT方式与主机、互联网通信

    1.首先配置物理主中机VMnet8的IP信息 主机物理IP为192.168.3.9

  4. ios换肤思想,及工具类

    // 实现原理及思路:不同种类的皮肤放在不同的文件夹下,用一个plist文件存放不同控制器下的控件的背景颜色 //plist文件名称为控制器的名称,内部的数据字典的key value对自定义一个命名规 ...

  5. 【T_SQL】 基础 续+++

    十五.T-SQL 编程 1.变量 (1)局部变量                    A.局部变量必须以标记@作为前缀 ,如@age.                    B.局部变量的使用也是先 ...

  6. 对于JSP的调试

    在eclipse中调试JSP 我换了图片但是网页中的图片却不变化 我删了工程里的图片还是没用 看了一下Tomcat根目录..貌似也是没有的.. 最后我考虑换了HTML中图片的名字..并且更改了替换的图 ...

  7. 关于解决haswell赛扬和奔腾 不能安装的问题

    打开EFI\CLOVER\config.plist,并找到KernelAndKextPatches字段,在子集内插入下面代码. <key>FakeCPUID</key> < ...

  8. jQuery-事件以及动画

    事件: 1.//方法1 $(window).load(function(){ }) window.onload=function(){ } //方法2 function one(){ alert(&q ...

  9. ural 1070. Local Time

    1070. Local Time Time limit: 1.0 secondMemory limit: 64 MB Soon the USU team will go to Vancouver to ...

  10. Modify a Stored Procedure using SQL Server Management Studio

    In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand  ...