Hibernate provides caching and snapshot mechanisms in order to improve performance.
Its cache is divided into first-level cache and second-level cache.
Hibernate first-level cache: When a Sql statement is executed in a transaction, the returned results are stored in the Map collection in the Session (of course, there are also snapshots).
Test: (all the following codes are in try/catch blocks)
Configuration config=new Configuration().configure();//configure()方法是加载src/hibernate.cfg.xml配置文件 SessionFactory sf=config.buildSessionFactory(); Session s=sf.openSession();//Session是高一级的对Connection的封装 Transaction tran=null; try { tran=s.beginTransaction(); //代码在此 tran.commit(); } catch (HibernateException e) { if(tran!=null){ tran.rollback(); } e.printStackTrace(); } finally{ s.close(); sf.close(); }
Query: including get(), load(), native Sql, HQL, Criteria (a more object-oriented query method than HQL)
//1.get(),load()方法测试 User u=(User) s.get(User.class, 1);//第一次查询生成SQL语句,并将结果放入缓存 User u1=(User) s.get(User.class, 1);//第二次查询并无生成SQL语句,但结果取自缓存 p(u==u1);//true //2.HQL查询 Query q=s.createQuery("from domain.User where id=1"); User u2=(User) q.uniqueResult();//第三次查询生成了SQL语句,但结果取自缓存 p(u2==u);//true //3.原生Sql SQLQuery q1=s.createSQLQuery("select * from User where id=1"); q1.addEntity(User.class); User u3=(User) q1.uniqueResult();//第四次查询生成了SQL语句,但结果取自缓存 p(u3==u);//true //4.Criteria查询 Criteria c=s.createCriteria(User.class); c.add(Restrictions.eq("id", 1)); User u4=(User) q1.uniqueResult();//第五次查询生成了SQL语句,但结果取自缓存 p(u4==u);//true
Summary query:
Added: save(), persist()
User user = new User();//对象的瞬态 user.setName("xiaobai"); user.setAge(121); s.save(user);//对象的持久态 s.persist(user);
The difference between the two methods here is: the problem of setting the primary key before executing the method and the problem of returning the primary key after executing the method.
1, persist(), persists a transient instance, but there is no guarantee that the identifier (the attribute corresponding to the identifier primary key) will be filled in to the persistent instance immediately, and the filling in of the identifier may be delayed. When it’s time to flush.
2. save(), generates a transient instance persistence identifier in time. It returns the identifier, so it will execute Sql insert immediately.
User u = new User(); u.setName("xiaobai"); u.setAge(121); s.save(u);//插入数据库,并将对象瞬态转为持久态,将返回对象存入缓存 User u1=(User) s.get(User.class, u.getId());//这次查询没有生成SQL语句,结果取自Session的缓存 p(u1==u);//true
Delete: delete()
User u=(User) s.get(User.class, 10);//Perform query operation
s.delete(u);//Convert the object’s persistent state to a free state
Of course, if you feel that in order to delete a piece of data, performing query operations will reduce performance, you can do this:
User u=new User();
u.setId(5);
s.delete(u);
Update: update()
User u=(User) s.get(User.class, 1);
u.setName("set");
But sometimes, we don’t need to execute s.update( Object) method, which involves a feature of the persistent state of the object (also [snapshot] plays a role in it):
When the object is persistent, when it updates data, the framework will compare it with the previous snapshot. If If they are the same, no action will be taken; if they are different, they will be automatically updated to the database.
//当然,也可以这么做 User u=new User();//对象的瞬态,不具备自动更新功能,需要我们手动update() u.setAge(1); u.setId(1); u.setName("1"); s.update(u);
Summary:
One point is very important: although the Sql statement is formed in the transaction, the database will only be actually operated after transaction.commit().
Hibernate needs to figure out [cache, snapshot, object three-state] and other things about database operations.
Three states of objects:
* Transient state: Not related to hibernate, no corresponding id in the database table
* Persistent state: Related to hibernate, there is a corresponding id---OID in the database table
* Free state : Not related to hibernate, there is a corresponding id in the database table