Eclipse quickly hibernate--2. Using HBM mapping file to develop

Source: Internet
Author: User
Tags constructor copy generator include sql tostring uuid root directory
This article is an article on Eclipse's quick start hibernate--1. The continuation of the Getting started example is mainly about how to use the HBM mapping file to generate common Java objects and data tables. You can refer to the 15th chapter of the Toolbox Guide, "HIBERNATE-compliant Java custom relational database persistence," HIBERNATE's own documentation. Similarly, this article does not talk much about theory, but gives a complete example to illustrate.  Related configuration Please refer to the previous article.  1. Create Project · Create a new Java project: Hibernatebegin_2, and note that creating a separate source folder and output folder is selected, adding the user library: Hibernate.  2. mapping File User.hbm.xml Create a new package, package name: Javamxj.hibernate, and then create a new file under this package, file name: User.hbm.xml. User.hbm.xml
<?xml version= "1.0" encoding= "GBK"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping dtd//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-2.0.dtd ">Compared to the User.hbm.xml file in the previous article, you can see that there are many additional settings added to the label.  Here <meta> the contents of the tags will be inserted into the class's Javadoc instructions. ·  <id> tags use uuid.hex to define the primary key generation algorithm, which uses the IP address, the JVM's startup time, the system time, and an accounting value to generate the primary key. ·  <column> in the <property> tab is used to generate columns in the datasheet.  3. Build file Build.xml  Copy the "hibernate.cfg.xml" configuration file from the previous article to the SRC directory. · In the project root directory to create a build.xml, this file contains four tasks, here will be used to "Generate-code", "Schemaexport" two tasks, as far as the usage can see the annotation. Note that the setting of the environment variable should conform to its actual configuration, for example, the setting of the library file directory is "D:/java/hibernate/lib", which follows the setting in the previous article.
Build.xml

<?xml version= "1.0" encoding= "GBK" ><project "Name=" "Hibernate" Help "default=" with tool development. ><!--environment settings, can be based on their own actual configuration change * * * * *--><!--source file directory, you can-> the project->java build path Changes--><property Name= "Src.dir" value= "./src"/><!--the Output class file directory, which can be changed through the Project-> property->java build Path--><property name= " Class.dir "value="./bin "/><!--library file directory--><property name=" Lib.dir "value=" D:/java/hibernate/lib "/>< !--defines classpath--><path id= "Project.class.path" ><fileset dir= "${lib.dir}" ><include name= "*.jar"/> </fileset><pathelement location= "${class.dir}"/></path><!--******************************* --><!--Use instructions--><!--************************************************* --><target name= "Help" ><echo message= "exploiting tools Hibernate"/><echo message= "----------- ------------------------"/><echo message=" "/><echo message=" provides the following tasks: "/><echoMessage= ""/><echo message= Generate-code--> runs Hbm2java, generating Java class file "Hbm.xml/><echo" using message= files GENERATE-HBM--> run Hibernatedoclet, generate Hibernate class mapping file "/><echo message=" Schemaexport--> run Schemaexport, Generate datasheet using Hbm.xml file "/><echo message=" "/></target><!--******************************************* --><!--Hbm2java task--><!--******************************************************* --><target name= "Generate-code" ><echo message= "run Hbm2java task to generate Java class files using hbm.xml files"/>< Taskdef name= "Hbm2java" classname= "Net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref= "Project.class.path" ></taskdef>· The final directory structure is as follows: 4.  Run Tasks · Right-click "Build.xml"-> "Run"-> there should be "ant build" and "Ant build ..." two menus, where "ant build" runs the default tasks directly, which means "help" tasks, and if you want to run other tasks, you can build by ant ...  "Menu selection. · A better way to do this is to click "Windows"-> "Show View" on the Eclipse main menu-> click on "Ant", which brings up the Ant view, in the blank space of the View window, right click, and select "Add Build File" in the pop-up menu. Then load the "build.xml" file in the root directory of the Hibernatebegin_2 project. The effect is as follows: In this way, to run a task, double-click the task in the Ant view directly.  Generate User.java  Double-click the "Generate-code" task and you should see the following output in the console: · If the SRC directory does not see the "User.java" This file, then select the SRC directory, and then click the "F5" function key to refresh the SRC directory, should be in the package "javamxj.hibernate" See "User.java". This file is generated by the "Hbm2java" generator according to the HBM file, as follows:
User.java

Package Javamxj.hibernate;import Java.io.serializable;import org.apache.commons.lang.builder.tostringbuilder;/** * * Run Hbm2java tasks, generate Java class files using hbm.xml files * @author javamxj (share java Fun) * @link blog:htpp://javamxj.mblogger.cn * htpp://blog . csdn.net/javamxj/* */public class User implements Serializable {/** identifier field/private String ID;/** persiste NT field */private String username; /** Persistent field */private String password; /** Full Constructor */public User (string Username, string password) {this.username = username; this.password = password ; }/** default constructor */Public User () {} is public String getId () {return this.id;} public void setId (String id) {th Is.id = ID; }/** * @param username */public String GetUserName () {return this.username.} public void Setusername (String username) {Thi S.username = Username; Public String GetPassword () {return this.password.} public void SetPassword (String password) {This.password = Passwor D Public String toString (){Return to New Tostringbuilder (this). Append ("id", GetId ()). ToString ();}
You can compare the "User.hbm.xml" file to see which attributes are converted into what code.     Generate datasheet   Start MySQL, you should make sure that you have a hibernatetest database, but you don't need to set up a datasheet this time.   Double-click the "Schemaexport" task and the console output is as follows. Note automatically generated SQL statements:    at the same time, in the project root directory, will also produce a "schema-export.sql" file (if not, press the F5 key to refresh the directory), this file is in the "Build.xml" Set in: Schema-export.sqldrop table if exists usertable2create table UserTable2 (   ID varchar () not null,    DDD varchar not null,   password varchar (in) Not null,   primary key (ID)   ·  switch to Database, you will find that the data table usertable2:  5 has been automatically generated. Test program   Well, copy the Test.java file from the previous article to the package "Javamxj.hibernate" and then right-click to run the file to see the data generated in the datasheet.     Summary      Well, take a look at the structure of the entire project, where "User.java" and "Schema-export.sql" two files are automatically generated, One thing to note is that the "User.java" file needs to be called to generate the "schema-export.sql" file, so the "Generate-code" task is performed before the "Schemaexport" task.     It is best to use the Xmlbuddy plug-in to edit the XML file, you can refer to:        about how to use hbm mapping file to generate ordinary Java objects and data tables , but also need to look at the reference document, but also to practice more, in practice, in practice to advance.     the next article will talk about using Xdoclet to develop hibernate.

Related Article

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.