Hibernate -------- Commonly used interfaces and classes

1 configuration


When config.configure() is used by default, the read configuration file is hibernate.cfg.xml.

But there are several configure() methods that can be used to configure custom XML files.


If the name of the .cfg.xml file is changed, the corresponding config should also be changed.






2 SessionFactory





Therefore, every time you want to create a session, you only need to call the method OpenSession of this factory class.

Of course, you can also write the closeSession() method into it.


3 Session




Example:


Equivalent to select statement


Equivalent to insert statement




Equivalent to select. insert.


Equivalent to select. drop 


Equivalent to select.



4. Transation 


As long as there is a problem with the code in the try and it enters the catch, then the commit() will not be executed, but the rollback() code will be executed. It is equivalent to returning to the step of session = null creation.

And the standard way of writing must have try-catch. Otherwise, there will be data errors.


Explanation of things being rolled back:

I have been working on a project with hibernate recently, and I have never understood the difference between session.beginTransaction().commit() and session.beginTransaction().rollback();

The behavior that a group of businesses handles as a whole is called a transaction. The business of this group can be processed successfully, and we can submit this transaction to save the result of the behavior you have done. But if any error occurs in the group, we consider the transaction unsuccessful and need to roll back to undo the previous operation. 
For example: you go to the bank to transfer money, we have two steps to transfer money, withdraw money from your account and then add money to his account. The bank must ensure that these two steps are carried out correctly. to be treated as a transaction. An error in any one of these steps means that the transfer fails, but maybe you have deducted money from your account at this time, and did not add money to his account? What to do? You will not. Therefore, the bank will roll back the transaction without saving the operation you just made, that is, it will return to the state before you did the transfer.


参考:https://www.cnblogs.com/demongao/p/6053853.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324705687&siteId=291194637