Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hibernate Util

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Hibernate Util". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Hibernate Util.

Hibernate provides many different ways to configure the framework, including programmatic configuration. The above code sets up connection pooling. Note that the in-memory database using HSQLDB requires the user name 'sa'. Also be sure to specify a space as the password. To start the automatic mode generation function of Hibernate, set the hibernate.hbm2ddl.auto property to 'creat-drop'.

My actual test project was to deal with large amounts of baseball data, so I added four mapped classes (Player, PintchingStint, BattingSint, and FieldStint). * create a session factory for Hibernate and insert it into the Hibernate Util class, which provides only one access method for the entire application of the Hibernate session. The code for Hibernate Util is as follows:

Import org.hibernate.*; import org.hibernate.cfg.Configuration; public class HibernateUtil {private static SessionFactory factory; public static synchronized Session getSession () {if (factory = = null) {factory = new Configuration () .buildSessionFactory ();} return factory.openSession ();} public static void setSessionFactory (SessionFactory factory) {HibernateUtil.factory = factory;}}

Because all code (production code that has been unit tested) gets the Hibernate session from Hibernate Util, it can be configured in the same location. Accessing the TestSchema class for unit testing of the * bits of the code will activate the static initialization program, which installs Hibernate and inserts the test SessionFactory into the Hibernate Util. For production code, the standard hibernate.cfg.xml configuration mechanism can be used to initialize SessionFactory.

So what are the external characteristics in unit testing? The following test code snippet is used to check the logic to determine where the player is in a baseball league game:

Public void testGetEligiblePositions () throws Exception {Player player = new Player ("playerId"); TestSchema.addPlayer (player); FieldingStint stint1 = new FieldingStint ("playerId", 2004, "SEA", Position.CATCHER); stint1.setGames (20); TestSchema.addFieldingStint (stint1); Set positions = player.getEligiblePositions (2004); assertEquals (1, positions.size ()); assertTrue (positions.contains (Position.CATCHER)) Thank you for your reading. The above is the content of "how to use Hibernate Util". After the study of this article, I believe you have a deeper understanding of how to use Hibernate Util, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report