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