Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Hibernate Util

Shulou Source: shulou.com Published: 2022-06-02 08:00:24 09月16日 Update

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!

Tags: Code test program configuration unit learning location content grade data baseball different candidate memory function password actually that is properties factory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Shulou Information Xiaomi MySQL