当前位置:网站首页>Hibernate entity class curd, transaction operation summary

Hibernate entity class curd, transaction operation summary

2022-06-25 23:34:00 _ Qilixiang

Full of dry goods !

Catalog

Add operation

save() Method

saveOrUpdate() Method

Inquire about

modify  

Delete operation

Save or update

Tool class encapsulation

Object state of entity class

Instantaneous state

Persistence state

Trusteeship

Hibernate Transaction operation of


Add operation

save() Method

saveOrUpdate() Method

 

Inquire about

result :

modify  

Use save() The method is completely consistent with this effect :

Direct... Is not recommended new Object readjustment update() Method , This results in a null value .

Delete operation

The first one is : according to ID Inquire about , Returns the object , call delete() Method to delete the object

The second kind :new object , call delete() Method to delete the object

         

Save or update

saveOrUpdate()

Instantaneous time : This method does the add operation

Persistent state : Modify the operating

In managed status : Modify the operating

Tool class encapsulation

package com.wl.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
 *  Extract tool class 
 */
public class HibernateUtils {

	private static final Configuration cfg ;
	private static final SessionFactory sessionFactory ;
	static{
		 cfg = new Configuration();
		 cfg.configure();
		 sessionFactory = cfg.buildSessionFactory();
	}
	// This method returns this sessionFactory object 
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
}

Object state of entity class

Instantaneous state

No in object id value , Object and the session There's no connection .

Persistence state

Among objects id value , Object and the session relation

Trusteeship

Among objects id value , Object and the session There's no connection

Hibernate Transaction operation of

Rule writing of transaction code

try{

 Open transaction 

 Commit transaction 

}catch(){

 Roll back the transaction 

}finally{

 Closing transaction 

}

原网站

版权声明
本文为[_ Qilixiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252020148049.html