Let's take a look at handle exception.

Source: Internet
Author: User

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:

  1. Public void save (employee transientinstance ){
  2. Log. debug ("Saving employee instance ");
  3. Try {
  4. Getsession (). Save (transientinstance );
  5. Log. debug ("Save successful ");
  6. } Catch (runtimeexception re ){
  7. Log. Error ("save failed", RE );
  8. Throw re;
  9. }
  10. }
  11. Public void Delete (employee persistentinstance ){
  12. Log. debug ("deleting employee instance ");
  13. Try {
  14. Getsession (). Delete (persistentinstance );
  15. Log. debug ("delete successful ");
  16. } Catch (runtimeexception re ){
  17. Log. Error ("delete failed", RE );
  18. Throw re;
  19. }
  20. }
  21. Public Employee findbyid (Java. Lang. String ID ){
  22. Log. debug ("getting employee instance with ID:" + id );
  23. Try {
  24. Employee instance = (employee) getsession (). Get (
  25. "APQP. HBM. admin. employee. Employee", ID );
  26. Return instance;
  27. } Catch (runtimeexception re ){
  28. Log. Error ("Get failed", RE );
  29. Throw re;
  30. }
  31. }

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:

  1. Departmentdao Dao = new departmentdao ();
  2. Transaction T = Dao. getsession (). begintransaction ();
  3. Dao. Save (new Department ("Purchasing Department "));
  4. T. Commit ();
  5. 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:

  1. Departmentdao Dao = new departmentdao ();
  2. Transaction T = NULL;
  3. Try {
  4. T = Dao. getsession (). begintransaction ();
  5. Dao. Save (new Department (" "));
  6. T. Commit ();
  7. Dao. getsession (). Close ();
  8. } Catch (hibernateexception e ){
  9. // Do some exception handler operate
  10. If (T! = NULL ){
  11. T. rollback ();
  12. }
  13. } Finally {
  14. Dao. getsession (). Close ();
  15. }

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 !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.