Full text copy from http://trace.javaeye.com/blog
Myeclipse has a hibernate plug-in that can quickly generate code related to hibernate operations, which is very convenient. When I was a beginner at hibernate, I called the automatically generated code and looked at the data of successful operations in the database. The joy is beyond words.
However, today, when I use hibernate for a project and downgrade the servlet environment, the servletexcpetion I need cannot be produced. When the database operation fails, does the servlet container let go of this error? Obviously not. However, looking at your own code, I cannot find a place where an error is forcibly thrown (eclipse will prompt you where exception is forcibly handled ). Let's take a look at the DAO class automatically generated by hibernate. There is no way to throw an exception:
- Public void save (employee transientinstance ){
- Log. debug ("Saving employee instance ");
- Try {
- Getsession (). Save (transientinstance );
- Log. debug ("Save successful ");
- } Catch (runtimeexception re ){
- Log. Error ("save failed", RE );
- Throw re;
- }
- }
- Public void Delete (employee persistentinstance ){
- Log. debug ("deleting employee instance ");
- Try {
- Getsession (). Delete (persistentinstance );
- Log. debug ("delete successful ");
- } Catch (runtimeexception re ){
- Log. Error ("delete failed", RE );
- Throw re;
- }
- }
- Public Employee findbyid (Java. Lang. String ID ){
- Log. debug ("getting employee instance with ID:" + id );
- Try {
- Employee instance = (employee) getsession (). Get (
- "APQP. HBM. admin. employee. Employee", ID );
- Return instance;
- } Catch (runtimeexception re ){
- Log. Error ("Get failed", RE );
- Throw re;
- }
- }
These methods internally process runtimeexception. However, some database operation methods such as Save () should throw an exception. According to the hibernate API, these methods will throw hibernateexception. However, when I found hibernateexception In the API, I found it to be a subclass of runtimeexception. No wonder I can't catch it!
Now I have figured out that when operating hibernate, you have to catch the exception where the exception should be thrown, for example:
- Departmentdao Dao = new departmentdao ();
- Transaction T = Dao. getsession (). begintransaction ();
- Dao. Save (new Department ("Purchasing Department "));
- T. Commit ();
- Dao. getsession (). Close ();
It can run without writing exceptions, but it does not meet the needs of the business logic. Therefore, catch hibernateexcpetion where exceptions may occur. However, where exceptions may occur? Taking into account the code of some experts and combining their characteristics, I decided:
- Departmentdao Dao = new departmentdao ();
- Transaction T = NULL;
- Try {
- T = Dao. getsession (). begintransaction ();
- Dao. Save (new Department (" "));
- T. Commit ();
- Dao. getsession (). Close ();
- } Catch (hibernateexception e ){
- // Do some exception handler operate
- If (T! = NULL ){
- T. rollback ();
- }
- } Finally {
- Dao. getsession (). Close ();
- }
In this way, I caught hibernateexcpetion.
Supplement: I have always been confused about how to handle exception, except for using system. err. println () and E. printstacktrace () report the exception, use throw to throw the exception, use try-catch to implement the IF-else-like steering function, and use try-catch to assign different values to the same variable (eg: try {I = 0} catch () {I = 1}). No other handle exception methods have been encountered. The Post recorded above broadened my horizons, especially the use of try-catch for log processing and rollback. Learning !!