标签:
对象的状态:createCriteria()
package test.hibernate.session; public class User { private Integer id; private String name; private byte[] b=new byte[1024 * 1024 * 5]; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test.hibernate.session"> <class name="User" table="session_user"> <!-- id元素用于映射主键。 子元素generator是用于指定主键生成策略的。 --> <id name="id" type="integer" column="id"> <generator class="native"></generator> </id> <property name="name" /> </class> </hibernate-mapping>
package test.hibernate.session; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { private static SessionFactory sessionFactory = new Configuration()// .configure()// .addClass(User.class)// 添加Hibernate实体类(加载对应的映射文件) .buildSessionFactory(); @Test public void testSave() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- // 构建对象 User user = new User();// 临时状态 // user.setId(UUID.randomUUID().toString());//id设为String类型 user.setName("张三"); // 保存 session.save(user);// 变成持久化状态 System.out.println("=======xxxxxxxxxxxxxx========"); // -------------------------------------------- session.getTransaction().commit(); session.close(); // 数据库里面有,session里没有,为游离状态 user.setName("李四"); System.out.println(user.getName()); } // update():把游离状态变为持久化状态 @Test public void testUpdate() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- User user = (User) session.get(User.class, 2); // user.setId(1); // user.setName("new name"); // session.flush();// 把user刷出到数据库 // // session.clear();// 变为游离状态,清除session中所有的对象 session.evict(user);// 清除一个指定的对象 user.setName("TestName"); session.update(user); System.out.println("==================="); // -------------------------------------------- session.getTransaction().commit(); session.close(); } // saveOrUpdate():把临时或游离状态转为持久化状态 /* * 本方法根据id判断对象是什么状态。 如果id为原始值(对象是null,原始类型数字是0)就是临时状态; 如果不是原始值就是游离状态 */ @Test public void testSaveOrUpdate() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- User user = new User(); user.setId(1); user.setName("testSaveOrUpdate"); session.saveOrUpdate(user); // -------------------------------------------- session.getTransaction().commit(); session.close(); } // delete():把持久化或游离状态转为删除状态 // 如果删除的对象不存在,会抛出异常 @Test public void testDelete() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- User user = (User) session.get(User.class, 8); session.delete(user); session.flush(); System.out.println("=========================="); // -------------------------------------------- session.getTransaction().commit(); session.close(); } // get():获取数据,是持久化状态 // 如果查询的对象不存在会抛出异常 @Test public void testGet() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- User user = (User) session.get(User.class, 8); System.out.println(user.getId()); System.out.println(user.getName()); // -------------------------------------------- session.getTransaction().commit(); session.close(); } // load():懒加载,获取数据,是持久化状态。延迟加载,用的时候才加载 /* * load()后返回的是一个代理对象,要求类(这里是User类)不能是final的, 否则不能生存子 类代理,就不能使用懒加载功能。 */ @Test public void testLoad() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- User user = (User) session.load(User.class, 7); System.out.println(user.getId()); System.out.println(user.getName()); // -------------------------------------------- session.getTransaction().commit(); session.close(); } @Test public void testBatchSave() throws Exception { Session session = sessionFactory.openSession(); session.beginTransaction(); // -------------------------------------------- for (int i = 0; i < 50; i++) { User user = new User(); user.setId(i); user.setName("ss0" + i); session.save(user); System.out.println("====================="); if (i % 10 == 0) { session.flush();// 先刷出 session.clear();// 再清空 } } // -------------------------------------------- session.getTransaction().commit(); session.close(); } }让懒加载失效的方式:
版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请注明出处:http://blog.csdn.net/lindonglian
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/46933809