Using MiddleGen-hibernate-r5 to generate hbm files and POJO files,
1 prerequisites 1.1 JDK (version 1.5 or later) is installed and environment variables are configured.
Download JDK to http://java.sun.com and configure environment variables (my PC Right-click-> properties-> advanced-> environment variables)
JAVA_HOME: JDK installation directory
CLASS_PATH: JDK installation directory \ lib
PATH: JDK installation directory \ bin
1.2 install ant and configure Environment Variables
Click the http://apache.mirror.phpchina.com/ant/binaries/apache-ant-1.7.1-bin.zip to download ant and unzip it, set the environment variables (my PC Right-> properties-> advanced-> environment variables)
ANT_HOME: extract the ant directory
Path = "ant Extract directory \ bin"
Then run the command and Enter cmd --- ant. The prompt is as follows, indicating that ant is correctly installed.
1.3 downloading MiddleGen
Click products.
2. jar packages
Jar packages required to generate hbm files
Because this example uses the Mysql database, you need:
Mysql-connector-java-5.1.11-bin.jar
Jar package required for POJO generation
Hibernate3.jar
Dom4j-1.6.1.jar
Click
Http://nchc.dl.sourceforge.net/sourceforge/hibernate/hibernate-distribution-3.3.1.GA-dist.zip download, dom4j-1.6.1.jar under hibernate-distribution-3.3.1.GA \ lib \ required (can also be downloaded through http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip)
Hibernate-tools.jar
Freemarker. jar
Click
Http://jaist.dl.sourceforge.net/sourceforge/jboss/HibernateTools-3.2.4.Beta1-R200810311334.zip download, unzip the jar package located in
Plugins \ org. hibernate. eclipse_3.2.4.Beta1-R200810311334 \ lib \ tools
Slf4j-api-1.5.2.jar
Slf4j-jdk14-1.5.2.jar
Slf4j-log4j12-1.5.2.jar
Click the http://www.slf4j.org/dist/slf4j-1.5.2.zip to download
3. Generation Process
Decompress the downloaded MiddleGen-Hibernate-r5 to any directory and create a new folder named mysrc under the build directory.
3.1 generate hbm files
Original content
<Property name = "database. script. file" value = "$ {src. dir}/SQL/$ {name}-mysql. SQL"/>
<Property name = "database. driver. file" value = "$ {lib. dir}/mysql-connector-java-3.0.16-ga-bin.jar"/>
<Property name = "database. driver. classpath" value = "$ {database. driver. file}"/>
<Property name = "database. driver" value = "org. gjt. mm. mysql. Driver"/>
<Property name = "database. url" value = "jdbc: mysql: // localhost/employee"/>
<Property name = "database. userid" value = "root"/>
<Property name = "database. password" value = "root"/>
<Property name = "database. schema" value = ""/>
<Property name = "database. catalog" value = ""/>
<Property name = "jboss. datasource. mapping" value = "mySQL"/>
Modify
<Property name = "database. driver. file" value = "$ {lib. dir}/mysql-connector-java-3.0.16-ga-bin.jar"/>
Change the connector to the mysql driver package name you imported (the driver should be placed under D: \ Middlegen-Hibernate-r5 \ lib)
<Property name = "database. url" value = "jdbc: mysql: // localhost/employee"/>
Set mysql url
<Property name = "database. userid" value = "root"/>
<Property name = "database. password" value = "root"/>
Set user name and password
MiddleGen Interface Description
After entering the Middlegen GUI, you can see that the table structure in the database has been imported to the MiddleGen operation interface. Select the table elements in the database table view, and we can adjust the attributes of each database table.
1. Domain Class Name corresponds to the POJO Class Name.
2. Key Generator
Primary Key Generator
Option description:
① Assigned. The primary key is generated by an external program and does not involve Hibernate.
② Hilo uses the Primary Key Generation Mechanism Implemented by the hi/lo algorithm. Additional database tables are required to save the primary key generation history status.
③ Seqhilo is similar to hilo. The primary key generation mechanism implemented by the hi/lo algorithm only stores the historical state of the primary key in Sequence, which is suitable for databases that support Sequence, such as Oracle.
④ The increment primary key increases in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause a problem: if multiple instances access the same database, because each instance maintains the primary key status, different instances may generate the same primary key, this causes duplicate primary key exceptions. Therefore, you must avoid using the same database to access multiple instances.
⑤ Identity uses the primary key generation mechanism provided by the database. For example, the primary key generation mechanism in DB2, SQL server, and Mysql.
6. sequence uses the sequence mechanism provided by the database to generate the primary key. For example, the sequence mechanism in oracle.
7. native is determined by Hibernate based on the underlying database and uses identity, hilo, and sequence as the primary key generation method.
⑧ Uuid. hex is generated by Hibernate Based on the 128-bit unique value algorithm to generate a hexadecimal value (encoded with a 32-Bit String) as the primary key.
⑨ Uuid. string is similar to uuid. hex, but the generated primary key is not encoded (Length: 16). problems may occur in some databases (such as PostgreSQL ).
External foreign uses the field of the External table as the primary key.
Generally, using uuid. hex to generate a primary key provides the best performance and database platform adaptability.
In addition, common databases such as oracle, DB2, SqlServer, and MySql provide easy-to-use primary key generation mechanisms (auto-Increase fields or sequence ). We can use the primary key generation method of generator-class = native in the primary key generation mechanism provided by the database.
3. The input section after key-generator. If you need to use a custom primary key generation algorithm, configure the primary key generator here. The primary key generator must implement the net. sf. hibernate. id. IdentifierGenerator interface.
4. Schema Name database Schema Name
5. Persister custom persistence implementation class name. If the system still requires a persistence layer implementation mechanism outside of Hibernate, such as getting the target dataset through the stored procedure, or even getting data from LDAP to fill our POJO.
6. Enable proxies whether to use a proxy (for delayed Loading [Lazt Loading])
7. If DynamicUpdate is selected, the generated update SQL statement does not contain the field attributes that have not changed. This improves the SQL Execution performance to a certain extent.
8. Whether the Mutable class is variable. The default value is "variable ). If you do not want the application to modify the corresponding data (for example, for the database view), you can cancel the selected state, and then delete and update of this type will be invalid.
9. Whether the Lifestyleinterface implements the lifestyle interface. The lifestyle interface provides a control mechanism in the Data solidification process. By implementing this interface, we can add a callback mechanism in database operations, such as triggering a specified operation before database operations.
10. Whether Validatableinterface implements the Validatable interface. By implementing the Validatable interface, we can verify the validity of the data before it is solidified to the database table. It should be noted that, by implementing the lifestyle interface, legality verification can also be performed before database operations. The difference is that the methods defined in the Validatable interface may be called multiple times. Therefore, we should avoid adding business logic verification to the implementation of the Validatable interface's validate method.
3.2 generate POJO
Copy the following jar package to the lib directory of the MiddleGen-Hibernate-r5 folder
Hibernate3.jar, hibernate-tools.jar, dom4j-1.6.1.jar, slf4j-log4j12-1.5.2.jar, slf4j-api-1.5.2.jar, slf4j-jdk14-1.5.2.jar, freemarker. jar
Open the build. xml file again
Modify row 212nd
<Target name = "hbm2java" description = "Generate. java from. hbm files.">
<Taskdef
Name = "hbm2java"
Classname = "net. sf. hibernate. tool. hbm2java. Hbm2JavaTask"
Classpathref = "lib. class. path"
/>
<Hbm2java output = "$ {build. gen-src.dir}">
<Fileset dir = "$ {build. gen-src.dir}">
<Include name = "**/*. hbm. xml"/>
</Fileset>
</Hbm2java>
</Target>
Is
<Target name = "hbm2java" description = "Generate. java from. hbm files.">
<Taskdef
Name = "hbm2java"
Classname = "org. hibernate. tool. ant. hibernatgateltask"
Classpathref = "lib. class. path"
/>
<Hbm2java destdir = "$ {build. gen-src.dir}">
<Configuration>
<Fileset dir = "$ {build. gen-src.dir}">
<Include name = "**/*. hbm. xml"/>
</Fileset>
</Configuration>
<Hbm2java jdk5 = "true"/>
</Hbm2java>
</Target>
Because the hbm file generated by Middlegen is referenced by the DTD of hibernate2, modify the DTD of hibernate2 in the generated hbm. xml from
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 2.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
Change
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
(That is, change 2 to 3 so that "Don't use old DTDs, read the Hibernate3.x Migration Guide!" will not appear !" Error)
Run ant hbm2java. The generated POJO file is located in the build \ mysrc \ model folder.