In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve Hibernate unit testing". In daily operation, I believe many people have doubts about how to achieve Hibernate unit testing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve Hibernate unit testing". Next, please follow the editor to study!
This article introduces that the most important thing in Hibernate unit testing is to keep the test cases independent. Because this method still involves a database, you need a way to clean up the database before each Hibernate unit test instance. There are four tables in my database schema, so I wrote the reset () method on TestSchemaz, which deletes all rows from the table that uses JDBC. Note that because HSQLDB recognizes foreign keys, the order in which tables are deleted is important. Here is the code:
Public static void reset () throws SchemaException {Session session = HibernateUtil.getSession (); try {Connection connection = session.connection (); try {Statement statement = connection.createStatement (); try {statement.executeUpdate ("delete from Batting"); statement.executeUpdate ("delete from Fielding"); statement.executeUpdate ("delete from Pitching"); statement.executeUpdate ("delete from Player"); connection.commit ();} finally {statement.close () } catch (HibernateException e) {connection.rollback (); throw new SchemaException (e);} catch (SQLException e) {connection.rollback (); throw new SchemaException (e);}} catch (SQLException e) {throw new SchemaException (e);} finally {session.close ();}}
When it is determined that a large number of deletions are performed in Hibernate 3.0, it should be possible to remove the direct JDBC bit from the application. At this point, you must obtain the database connection and submit the SQL directly to the database. In order to free resources, it is sufficient to just close the session while ensuring that the connection is not closed. Out of the habit of writing a lot of JCBC code by hand for development, * versions have closed JDBC connections. Because the connection pool created by configuring Hibernate has only one link, it completely destroys the test after *. We must pay attention to this situation! Since you cannot determine the state of the database while the test class is running (imagine running all the test instances), you should include database cleanup in the setUp () method, as follows:
Public void setUp () throws Exception {TestSchema.reset ();} at this point, the study on "how to implement Hibernate unit tests" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.