My OR Mapping Tour (1)

xiaoxiao2021-03-06  68

First of all, I would like to thank my TEAM aware of the shortcomings of traditional JDBC development, and began to pay attention to the results of O / R mapping. Speaking of O / R Mapping, I think it is a good choice when the object database has not really mature, look at the Sun's JDO 2.0 specification, its upcoming release will push this field to a higher realm. Let's take a look at our open source community, hey! Found Hibernate! From now on, my focus will be placed on Hibernate, an excellent O / R mapping tool.

Let us distinguish between several nouns before you have not officially started trip. POJO: On Hibernate represents the value object that contains the most basic operations of SETER, GETER. BO: represents the value object containing some business logic, its role domain, that is, BO can pass to the UI layer while serving as a lasting class. PO: Represents a persistent object, which is incorporated into the Hibernate management framework. To a certain extent, it can be interchanged with the concept of value objects, and the value object has been processed by hibernate, it will become PO. Hibernate profile: hibernate.cfg.xml or hibernate.properties, but it is recommended to use XML format. Mapping file * .hbm.xml: The role of the mapping file is to bind POJO to the relational database data as a bridge. In addition, it is not a good thing for the table in the database. Fortunately, there are also a group of people who have the same idea in the open source community. They have developed HibernateSynchronizer mapping tools to http://www.binamics.com/hibernateSync/ Eclipse2.1 / download.

Ok, now go to http://www.hibernate.org to download the Hibernate development package (the current version is 2.1.6). Then open Eclipse 2.1 and install HibernateSynchronizer in the Update Manager. It is said that Eclipse 3.0 has built-in mapping tools, but I didn't try it. After installation, create a web application HibernateTest, then add Hibernate library hibernate-2.1.6/ hibernate-2.1 / hibernate2.jar and its dependencies, database connection packages, strongly recommended to put Hibernate-2.1. All class libraries under 6 / hibernate-2.1 / lib load, as shown below:

Then use HibernateSynchronizer to generate hibernate.cfg.xml files, new -> Others-> Hibernate Configuration File, I am using the SQL Server database, each configuration parameter is shown below:

Generated hibernate.cfg.xml file:

In the SRC directory of the project, new four packages are new, Bo, Bo.Base, bo.mapping, com.dao, what role, when they will present one by one. Do one thing before starting the mapping file, configure HibernateSynchronizer for the application hibernateTest, I prefer to write DAO, so I don't have Data Access Objects, and the other parameters are as follows:

The mapped table The autoinfo structure is as follows, the ID is its primary key:

New -> Others-> Hibernate Mapping File, After the Parameters Refresh, select the table you want to map, pay attention to the 800 * 600 to map the file! Otherwise, some buttons do not appear, each configuration parameter is shown below: Finally, finally, the mapping file autoinfo.hbm.xml finally came out!

<> Property column = "id" length = "18" name = "id" not-null = "true" type = "integer" />

<> Property column = "id" length = "18" name = "id" not-null = "true" type = "integer" /> need to modify the autoinfo.hbm.xml file as It defines the primary key and puts:

Replace with:

Make persistent efforts to generate POJO for the mapping file. Modify the Hibernate.cfg.xml file in the tab, add the files just mapped . Then click AutoInfo.hbm.xml Right-click in "Package Resource Manager", Hibernate Synchronizer -> Synchronize Files. Take a look at the package in the SRC:

O / R mapping journey first here, the road behind it will get more and more bumps! are you ready?

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-92262.html

New Post(0)