?
Hibernate O / R mapping three basic specifications
1 Introduction
In the Java environment, there are multiple object relationship mapping methods such as entity beans, OJB, JDO, JDBC, etc. Hibernate is a new O / R mapping tool not only provides mapping from the Java class to data sheet, but also provides data query and recovery. Hibernate is very easy to learn. At present, there are many peers that have been or are using Hibernate to implement projects; but Hibernate has many rules that are not known or very applicable. The purpose of this article is to summarize these rules and want to be beneficial to your development and design.
First of all, this article is an entry-level article in Hibernate. Please don't have to look down.
2. ?? Basic
Rules 1: Database profile name is not hibernate.properties or hibernate.cfg.xml
Due to the impact of Hibernate official document (this article refers to the documentation of www.hibernate.org), many Hibernate developers mistakenly think that the system must have a file called hibernate.properties or hibernate.cfg.xml, and must be placed in the root of Classes Under contents. In fact, this file is just a general profile, the name can be taken by yourself, the file location can be fixed, and in the multi-database environment, there can be multiple profiles, so that the program can connect multiple databases. Just you need special processing when you create a session factory, and the code is now as follows:
// Profile name
Protected static string config_file = "my_hibernate.config";
// Configure the file directory, you can consider the storage of all configuration files in this directory, where virtual machine parameters are used here
String configPath = system.getProperty ("my.config");?
// Create a session factory ?????
SessionFactory = (New Configuration (). configure (new file (configpath file.separator config_file)) .BUILDSessionFactory ();
?
? Tail 2: Small project, .hbm.xml file can be merged into a file
Similarly, because of the impact of Hibernate official documents, many Hibernate developers mistakenly think that Hibernate's object relationship map must have a table corresponding to an object and put the mapping file and object files in the same directory. In fact, this mode is designed to normative consideration, because many people have to maintain the same document in a multiplayer to maintain conflicts; when the project is relatively small, the maintenance personnel of the configuration file are small, we can completely. The HBM.xml file is merged into one or several files. According to the author's project design and development experience, it is best to set a few.hbm.xml files, each maintained one. HBM.XML file, each maintained one. Of course, in order to facilitate classification and management, each person's work should be divided according to logic.
The file configuration example in large projects is as follows:
The file configuration example in a small project is as follows:
It should be noted that from the analysis of the Hibernate source code, when specifying a map file, you cannot use absolute directories, you can only use a relative directory; and the system is the current directory with the Classes root directory.
?
Truth 3: The object relationship mapping is based on .hbm.xml file, not based on the PO file
The law means that the system starts checking if your object relationship mapping is correct, is checked by .hbm.xml; object file's Property can be more than the Property of the mapping file, but not less. If you have the following mappings:
???????
???????????
???????????????
????????????
???????
???????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????
???????
???????????????????
After the system starts, check the database and other configurations, will first check the file-free ID attribute and the corresponding GET / SET method in the user object, if not, if there is an error, check there is no Name property And the corresponding GET / SET method ... until the file ends. It should be noted that if the property is Name, the GET method name must be getName (), the SET method name must be setName (), otherwise the error will be reported. If the User object has four attributes and corresponding methods in addition to ID, Name, Loginname, Passwd, there is other properties and methods, will there be an error? No, because the object relationship mapping is based on .hbm.xml file, Instead of being based on the PO file. You can take advantage of this feature in your project to achieve your own more special needs. For example, you can join the UpdatedPasswd in the User object to indicate the changed password, which is constructed in the FORMBEAN to use, and the valueObject to be used in the business layer. Of course, there is also a shortcoming of doing so, it needs to be determined according to your actual situation.
3.?? Summary
The above is the summary of the knowledge points of the author's knowledge points to the Hibernate mapping, I hope to help you. If you find an error or have your own point of view, welcome to discuss.
HONGBO781202, also known as hongsoft, professional programmer, research area: 1) Based on workflow BPM system research 2) Java-based information security
Technology. Welcome to discuss all aspects of Java related aspects of technology and market issues hongbosoftware@163.com
?
?
?