Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Hibernate Mapping

Shulou Source: shulou.com Published: 2022-06-02 08:00:23 10月04日 Update

This article mainly explains "how to realize Hibernate mapping". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to realize Hibernate mapping" together.

I was impressed with Hibernate, but one problem I encountered was trying to insert an indirect layer when using Hibernate for data mapping for unit testing. This additional layer is very fragile and difficult to write. The actual deployment version simply passes through Hibernate specific implementations. Worse, the mock version is more complex than the real "production" version simply because it has no basic object memory and Hibernate mapping.

I also use a lot of complex Hibernate queries and want to unit test important parts of my application. However, it is not a good idea to test a live database because this almost always creates maintenance problems. Also, since tests are independent of each other, using the same primary key in the test context data means that code must be created to clean up the database before each test, which becomes a real problem when a large number of relationships are involved.

By using HSQLDB and Hibernate's powerful pattern generation tools, you can unit test the application mapping layer and find countless bugs in object queries that were previously impossible to do manually. With the following technical overview, you can test your entire application during development without damage in the test active area.

Set HSQLDB

HSQLDB 1.7.3.0 version was previously used. To use the in-memory version of the database, you need to activate the static loader for org.hsqldb.jdbcDriver. When you get a JDBC connection, you can use a JDBC url such as jdbc:hspldb:mem:yourdb, where 'yourdb' is the name of the in-memory database you want to use.

Because I use Hibernate (3.0 beta 4), I have little contact with the actual live JDBC object. Instead, I can let Hibernate do a lot of heavy lifting, including automatically creating database schemas from Hibernate mapping files. Because Hibernate creates its own proprietary connection pool, it automatically loads the HSQLDB JDBC driver based on configuration code in the TestSchema class. Here is the static initializer for this class.

public class TestSchema {

static {

Configuration config = new Configuration().

setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect").

setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver").

setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:baseball").

setProperty("hibernate.connection.username", "sa").

setProperty("hibernate.connection.password", "").

setProperty("hibernate.connection.pool_size", "1").

setProperty("hibernate.connection.autocommit", "true").

setProperty("hibernate.cache.provider_class", "

org.hibernate.cache.HashtableCacheProvider").

setProperty("hibernate.hbm2ddl.auto", "create-drop").

setProperty("hibernate.show_sql", "true").

addClass(Player.class).

addClass(BattingStint.class).

addClass(FieldingStint.class).

addClass(PitchingStint.class);

HibernateUtil.setSessionFactory(config.buildSessionFactory());

}

Thank you for reading, the above is "Hibernate mapping how to achieve" content, after the study of this article, I believe we have a deeper understanding of how to achieve Hibernate mapping, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Test data program database version problem unit actual object that is application learning application complexity code memory content situation pattern static Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Redmi Shulou Information Linux