Hibernate OR mapping three basic specifications

xiaoxiao2021-03-05  26

Hibernate O / R Mapping Three Best Subject 1. Introduction In the Java environment, there are multiple object relationship mappings, 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.

2. Basic Target No. 1: Database Profile Name may not hibernate.properties or hibernate.cfg.xml Due to the impact of Hibernate official document (this article www.hibernate.org), many Hibernate developers mistakenly think the system must There are files named hibernate.properties or hibernate.cfg.xml, and must be placed under the root of Classes. 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, code examples, such as: // Profile name protected static string config_file = "my_hibernate.config"; // Configure file directory, you can consider storing all configuration files in this directory, this at the use of 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 ( ); Second: Small project, .hbm.xml file can be merged into a file also due to the impact of Hibernate official documents, many Hibernate developers mistakenly think of Hibernate's object relationship mapping must have a table corresponding to an object, and will Map files and object files are placed 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: < Mapping resource = "COM / Hongsoft / Test / Persistence / Animal.hbm.xml" /> Sample of file configuration in small projects is as follows: To note that the analysis of the Hibernate source code can be known, specify the mapping file When you use absolute directories, you can only use a relative directory; and the system is the current directory with the Classes root. Truth 3: Object Relationship Mapping is based on .hbm.xml file, instead of being based on the PO file, the law means: System start checking if your object relationship mapping is correct, it is .hbm.xml The benchmark is checked; the object file's property can be more than the Property of the mapping file, but not less.

If you have the following mapping: system startup After checking the database and other configurations, it will first check that there is no ID attribute and the corresponding GET / SET method in the User object. If there is no error, if there is an error, check if there is Name property and 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 summary of the knowledge points of the author's knowledge maps to the Hibernate mapping, I hope to help you. Welcome if you find an error or your own point of view, welcome to discuss

I don't know where to turn it from the Internet ... ^ _ ^

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

New Post(0)