Hibernate O/R mapping three basic rule

Source: Internet
Author: User
Tags benchmark generator
-->
-->

Hibernate o/R mapping three basic rule

1. Introduction

In the Java environment, there are a variety of object-relational mapping methods, such as entity beans, OJB, JDO, JDBC, and so on. Hibernate is a new O/R mapping tool that not only provides mappings from Java classes to datasheets, but also provides mechanisms for querying and restoring data. Hibernate is very simple and easy to learn, there are many peers in the country have been or are using Hibernate development project; But hibernate has many rules that are unknown or rarely used. The purpose of this article is to summarize these rules, hoping to benefit your development and design.

First of all , this article is Hibernate entry level article, Master Please do not look down.

2. basic rule

Rule one : The database configuration file name may not be hibernate.properties or Hibernate.cfg.xml

Due to Hibernate official documentation (this article refers to hibernate.org/">www.") hibernate.org documents), many Hibernate developers mistakenly assume that the system must have a file named Hibernate.properties or Hibernate.cfg.xml, and must be placed in the root directory of the classes. In fact, the file is just a generic configuration file, the name can be taken by itself, the file location can be set itself, and in a multiple database environment, you can have more than one configuration file, so that the program can connect multiple databases. It's just that you need special handling when creating a session factory, and The code example is as follows:

Configuration file name

protected static String Config_file = "My_hibernate.config";

Profile directory, consider storing all of the system's configuration files in this directory, using virtual machine parameters

String Configpath = System.getproperty ("My.config");  

Create a session Factory

Sessionfactory = (new Configuration (). Configure (new File (Configpath+file.separator+config_file)). Buildsessionfactory ();

Rule Two: in small projects,. hbm.xml files can be merged into one file

Also due to the impact of Hibernate official documents, many hibernate developers mistakenly assume that Hibernate object mapping must have a table corresponding to an object and put the mapping file and object files in the same directory. In fact, the model is designed for normative reasons, because it is easy for multiple people to maintain the same file in a multiple-person project, and when the project is less than an hour, the profile has fewer maintenance personnel, and we can merge the. hbm.xml file into one or several files. According to the author's own project design and development experience, preferably in the project are several people engaged in R/O mapping and persistent work, then set several. hbm.xml file, each maintenance one. Of course, in order to facilitate the classification and management, each person's work should be logically divided.

The file configuration in a large project is as follows :

<Hibernate-configuration>

<session-factory>

<!-Map File Scatter-->

<mapping resource= "Com/hongsoft/test/persistence/cat.hbm.xml"/>

<mapping resource= "Com/hongsoft/test/persistence/animal.hbm.xml"/>

</session-factory>

</Hibernate-configuration>

Examples of file configurations in small projects include the following :

<Hibernate-configuration>

<session-factory>

<!-Mapping File Set-->

<mapping resource= "Res_hibernate_map.xml"/>

</session-factory>

</Hibernate-configuration>

It should be noted that, from the analysis of the Hibernate source code, the specified mapping file, can not use absolute directory, only relative directory, and the system to the classes root directory as the current directory.

Rule three : Object-relational mappings are based on the. hbm.xml file, not the PO file as the benchmark

The law means that when the system starts to check that your Object relational mapping is correct, it is checked by the. hbm.xml; The object file can have more property than the mapping file, but not less. If you have the following mappings:

<class name= "Com.hongsoft.test.User" table= "Res_user" >

<id name= "id" column= "id" type= "long" unsaved-value= "any" >

<generator class= "Assigned" >

</generator>

</id>

<property name= "name"/>

<property name= "LoginName"/>

<property name= "passwd"/>

</class>

System boot , check database and other configuration is correct, this file will be used as a benchmark to first check for the user object with no id attribute and corresponding Get/set method, if not, and if it is correct, check for the name attribute and the corresponding Get/set method ... until the end of the file. Note that if the property is name, the Get method name must be GetName (), the Set method name must be SetName (), or it will be an error.

If the user object has Id,name,loginname, passwd four properties and corresponding methods, do you have any other properties and methods? No, because the object-relational mappings are based on the. hbm.xml file, not the PO file as the benchmark . You can take full advantage of this feature in your project to achieve your own more specific needs. For example, you can add updatedpasswd to the user object to represent the changed password, which constructs the Formbean to use in the presentation layer and the Valueobject to be used at the business level. Of course, this has its drawbacks, it needs to be based on your actual situation.

3. Summary

The above is the author of the Hibernate mapping Easy to understand the error of the knowledge point of the summary , hope to help you . If you find a mistake or have your own opinion , welcome to the discussion.

hongbo781202, also known as Hongsoft, professional programmer, research field: 1 Research on BPM system based on workflow 2 information security based on Java

Technology. Welcome to discuss Java related technical and market issues hongbosoftware@163.com

-->
-->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.