博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate CURD
阅读量:5272 次
发布时间:2019-06-14

本文共 5905 字,大约阅读时间需要 19 分钟。

package test.com.lesson5;import javax.print.attribute.standard.Finishings;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.AnnotationConfiguration;import com.lesson5.User;import junit.framework.TestCase;public class TestUser extends TestCase {        public void testCreate(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            //瞬时态            User user = new User();            user.setName("test");            //执行insert            session.save(user);                        tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }        public void testGet(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();                Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            //从数据 库读出的user是持久态            //get方法先查一级缓存,再查二级缓存,最后再查数据 库            User user = (User) session.get(User.class,0);            System.out.println("name=" + user.getName());                        //从缓存中获取            User user2 = (User) session.get(User.class,0);            System.out.println("name=" + user2.getName());                        tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }        public void testLoad(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();                Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            //load先查一级缓存,没有创建代理对象,访问对象属性时才进行实际查询            //load方法如果没有找到记录则抛出            //org.hibernate.ObjectNotFoundException: No row with the given identifier exists            User user = (User) session.load(User.class,1);            System.out.println("name=" + user.getName());                        tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }        public void testDelete(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();                Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            User user = (User) session.get(User.class,0);            System.out.println("name=" + user.getName());                        session.delete(user);            tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }        public void testSaveOrUpdate(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            //瞬时态            User user = new User();            user.setId(123);            user.setName("test123");            //merge方法执行后,user不会纳入session管理,与saveOrUpdate相反            session.saveOrUpdate(user);                        User user2 = (User) session.get(User.class,123);            System.out.println("hashcode=" + user.hashCode());            System.out.println("hashcode=" + user2.hashCode());            //hashcode 一样                        tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }        public void testMerge(){        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction tx = session.beginTransaction();                try {            //瞬时态            User user = new User();            user.setId(123);            user.setName("test123");            //merge方法执行后,user不会纳入session管理,与saveOrUpdate相反            session.merge(user);                        User user2 = (User) session.get(User.class,123);            System.out.println("hashcode=" + user.hashCode());            System.out.println("hashcode=" + user2.hashCode());            //hashcode 不一样                        tx.commit();        } catch (Exception e) {            tx.rollback();                        e.printStackTrace();        }finally{            session.close();        }    }}
package com.lesson5;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class User {    private int id;    private String name;        @Id    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }        }
org.hibernate.dialect.MySQLDialect
jdbc:mysql://127.0.0.2:3306/test
root
root
com.mysql.jdbc.Driver
true
true
update

 

转载于:https://www.cnblogs.com/xuruhong/p/3308632.html

你可能感兴趣的文章
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>
vue和react的区别
查看>>
第十一次作业
查看>>
负载均衡策略
查看>>
微信智能开放平台
查看>>
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
c# 文件笔记
查看>>