1 when we call the configuration Config=new configuration (). Configure ();
Hibernate automatically searches the current classpath for the Hibernate.cfg.xml file and reads it into memory as the underlying configuration for subsequent operations.
We can also specify the profile name if you do not want to use the default Hibernate.cfg.xml file as the configuration file:
Sessionfactory
Sessionfactory is responsible for creating an instance of the session. We can create sessionfactory through configuation instances;
Configuration Config=new configuration (). Configure ();
Sessionfactory sessionfactory=config.buildsessionfactory ();
The configuration instance config constructs the Sessionfactory instance and returns it based on the current config information. Once the sessionfactory is built, specific configuration information is given. That is, any subsequent changes to config will not affect the Sessionfactory instance (Sessionfactory) that has already been created. If you need to use the sessionfactory based on the modified config instance, you need to rebuild a sessionfactory instance from config.
Session
The Session is the basis for the persistence layer operation, equivalent to the connnection in JDBC.
Configuration Config=new configuration (). Configure (); Read the default Hibernate.cfg.xml file
Sessionfactory sessionfactory=config.buildsessionfactory (); Creating an instance of Sessionfactory with Config
Session session=sessionfactory.opensession (); Get session
After that, we can invoke the Save,find,flush method provided by the session to complete the persistence operation:
See Example Save:
TUser user=new TUser ();
User.setname ("Yuanliang");
Session.save (user);
Session.flush (); The//session.flush method forces the database to synchronize, forcing Hibernate to immediately synchronize the user instance to the database. The Flush method is also automatically executed when the object is submitted and when the session is closed
Find ()//This method returns a list
List List=session.find (from TUser as Tu Order by tu.name ASC);
Load ()//This method returns an object
Customer c= (Customer) session.load (customer.class,customer_id);
To modify the data:
1. The data to be modified is read from the database and assigned to the persisted class corresponding to the table
Query query = Session.createquery ("From User as u where list = Query.list ();
User usr = (user) list.get (0);
2. Modify the resulting persisted object
Usr.setusername ("look");
3. Start Transaction Management
Transaction ts = session.begintransaction ();
4. Submit the assigned persisted object to the session
Session.save (USR);
5. End transaction management and submit to database
Ts.commit ();
-------------------------------
public void Update () {
Integer integer (2);
Transaction ts = null;
try {
Session session = Hibernatesessionfactory.currentsession ();
Query query = Session.createquery ("From User as u where list = Query.list ();
User usr = (user) list.get (0);
Usr.setusername ("look");
ts = session.begintransaction ();
Session.save (USR);
Ts.commit ();
} catch (Hibernateexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
try {
Ts.rollback ();
} catch (Hibernateexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
}
}
Hibernate Sessionfactory creation and session acquisition