Automatically generate HBM2DDL and database scripts from HBM files using the Hbm2java and Pojo tools in Hibernate-tools in the MyEclipse Web Project/java Project

Source: Internet
Author: User
Tags create database schema generator

First of all, I have to spit out the groove, this ant management deployment project tools, and Hibernate just learned, import me this simple problem whole day more, it is a bit low efficiency. During these two days, ① learned about Ant, learned about the flexible steps in ant, and learned some basic things about the Build.xml file. ② also learned to complete a project deployment in an integration tool such as MyEclipse. ③ and in both cases, use the Hbm2java and HBM2DDL tools in Hibernatetools to automatically generate Pojo and SQL files (that is, data tables) based on the object-relational mapping file.

One, in a Web project, use these two tools to generate Pojo and SQL

1, the structure of the project:


2, the project needs to import the jar package:


①Freemarker.jar must be imported, or will be reported abnormal

② Hibernate-tools.jar, which provides Hbm2java and HBM2DDL tools, can be used to generate Pojo and SQL through the mapping file.

③Log4j.jar

④Mysql-connector-java.jar. It is also possible to use other database drivers.

⑤slf4j related three jar packages are best to import all in. Relevant knowledge and learning, you can check the official documents.

⑥ All the remaining jar packages are all jars of lib/required in Hibernate.

3,hibernate configuration used to initialize

(configuration file, or XML, both can, but usually choose XML more general and more aspects)

In this demo, these two files are used. See the contents of the two files in detail:


Hibernate.properties (Database related information)

hibernate.dialect=org.hibernate.dialect.mysqldialecthibernate.connection.driver_class= Com.mysql.jdbc.driverhibernate.connection.url=jdbc:mysql://localhost:3306/testhibernate.connection.username= Roothibernate.connection.password=roothibernate.show_sql=truejavax.persistence.validation.mode=none
Hibernate.cfg.xml ()

<?xml version= ' 1.0 ' encoding= ' UTF-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
4, Object relational mapping file Hbm.xml


The two tools in Hibernatetools, it is the reference to these two files to generate Pojo and SQL

User.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
<!--pay special attention here, this indicates the package where the Pojo object is located, and when generating Pojo with Hbm2java, it will be added to the Java file header with a pack XXX; --><class name= "user" table= "user" dynamic-insert= "true" dynamic-update= "true" ><id name= "UserId" type= " Integer "><generator class=" Identity "/></id><property name=" Account "type=" string "length=" 64 "/ ><property name= "Name" type= "string" length= "all"/><property name= "nickname" Type= "string" length= "128"/ ><property name= "status" type= "integer" length= "1"/><property name= "Phones" type= "string" length= "128"/ ><property name= "Emails" type= "string" length= "all"/><set name= "addresses" table= "Address" inverse= "true "Cascade=" All "sort=" unsorted "order-by=" zipcode ASC "><key column=" userId "/><one-to-many class=" Address "/></set></class>


Address.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

5,build.xml file

(This file is an ant script, writing hbm2ddl and Hbm2java tasks, can be more flexible to manage the project )

<?xml version= "1.0" encoding= "UTF-8"? ><project name= "Hibernate" default= "compile" basedir= "."                       ><echo>===========================================================</echo><echo>| Hibernate |</echo><echo>=========================================================== </echo><property name= "src" value= "src" ></property><property name= "Src.java" value= "src"/> <!--note here that the source code package location specifies the root location, for the following reasons--><property name= "src.conf" value= "src/conf"/><property name= "Lib.dir" Value= "Webroot/web-inf/lib"/><property name= "Classes.dir" value= "webroot/web-inf/classes"/><property Name= "Schema.dir" value= "db"/> <!--classpath--><path id= "classpath" ><pathelement location= "${ Classes.dir} "/><fileset dir=" ${lib.dir} "><include name=" **/*.jar "/></fileset></path> <!--prepare catalogue--><target name= "prepare" ><mkdir dir= "${classes.dir}"/></targEt><target name= "Prepare-shcema-dir" ><mkdir dir= "${schema.dir}"/></target><!--delete directory, Used to recompile--><target name= "clean" description= "Delete all compiled files" ><delete dir= "${classes.dir}"/></target ><!--compiling source files in Src.java--><target name= "Compile.java" depends= "Prepare" description= "compiling Java source code" >< Javac srcdir= "${src.java}" destdir= "${classes.dir}" debug= "on" ><classpath refid= "Classpath"/></javac ><copy todir= "${classes.dir}" ><fileset dir= "${src.conf}" ><include name= "**/*"/></fileset ><fileset dir= "${src.java}" ><exclude name= "**/*.java"/></fileset></copy></target ><!--Compile all source files (Src.java)--><target name= "Compile" depends= "Compile.java"/><!--create. Java form *. Hbm.xml--><target name= "Hbm2java" depends= "Compile" description= "Generate Java source from the O/R mapping Files" & Gt;<taskdef name= "Hbm2java" classname= "Org.hibernate.tool.ant.HibernateToolTask" Classpathref= "Classpath"/>
6, run the process diagram


7, run the result diagram:


For example, both Pojo and SQL have been successfully generated.

--------------------------------------------------------------------------------------------------------------- ---------------------------

In this special thanks to other seniors for sharing, from his sharing and my groping, let me know how to use Hibernatetools in a Web project

Two tools to generate what is needed. Of course, this is just into the door, Ascension also requires follow-up learning and practice

--------------------------------------------------------------------------------------------------------------- ---------------------------

Second, in a Java project, use these two tools to generate Pojo and SQL

In this respect, the previous blog post has been reproduced. The note is similar, but the configuration of individual details needs to be noted. Do not repeat here.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Automatically generate HBM2DDL and database scripts from HBM files using the Hbm2java and Pojo tools in Hibernate-tools in the MyEclipse Web Project/java Project

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.