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:
If you have the following mapping:
I don't know where to turn it from the Internet ... ^ _ ^