This article is the last article "
Eclipse Quick Hibernate - 1. Getting Started Examples, mainly how to generate normal Java objects and data sheets using the HBM mapping file. You can refer to Hibernate's own document "Hibernate - the Java's Relationship Database Holds Database Purchase" section - Toolbox Guide "section. Similarly, this article did not talk about theory, just give a complete example. Related configuration, please refer to
Previous article.
Create a project
· New Java project: hibernatebegin_2, pay attention to "Create a separate source folder and output folder" while adding "user library": hibernate.
2. Mapping file User.hbm.xml
· Create a new package, package name: javamxj.hibernate, then create a new file under this package, file name: user.hbm.xml.
User.hbm.xml
XML Version = "1.0" encoding = "GBK"?>
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
Run HBM2JAVA tasks, generate Java class files with HBM.xml files
@Author Javamxj (Share Java Happy)
@LINK BLOG: htpp: //javamxj.mblogger.cn
Htpp: //blog.9cbs.net/javamxj/
meta>
id>
@ param User Name meta>
Property>
Property>
clas>
hibernate-maping>
●
The user.hbm.xml file in the last article can find a lot of additional settings on the label.
· Here the content in the tab will be inserted into the class's javadoc description.
·
3. Build a file build.xml
· Copy the "hibernate.cfg.xml" profile in the last article to the src directory.
· Create a build.xml in the root directory, this file contains four tasks, here you use "Generate-Code", "SchemaExport" two tasks, as for usage to be comments. Pay attention to the setting of the environment variable to meet your own actual configuration, such as the library file directory settings are "D: / java / hibernate / lib", is along
The setting in the last article.
Build.xml
XML Version = "1.0" encoding = "GBK"?>
properties -> Java build path ->
fileset>
path>
target>
ClassName = "Net.sf.hibernate.tool.hbm2java.hbm2javatask" ClasspathRef = "Project.class.path"> taskdef> fileset> hbm2java> target> ClassName = "xdoclet.modules.hibernate.hibernatedoclettask" ClasspathRef = "Project.class.path"> taskdef> ExcludedTags = "@ version, @Author, @ TODO" force = "true" encoding = "gbk" verbose = "true"> fileset> hibernatedoclet> target> ClassName = "Net.sf.hibernate.tool.hbm2ddl.schemaexporttask" ClasspathRef = "Project.class.path"> taskdef> Text = "no" drop = "no" output = "schema-export.sql"> schemaexport> target> provject> · The last directory structure is as follows: 4. Run the task · Right-click "Build.xml" -> "Run" -> There should be "ANT Build" and "Ant Build ..." two menus, where "Ant Build" is directly running the default task, here is "HELP" "Task; if you want to run other tasks, you can choose from" Ant Build ... "menu. · Here is a better way, click "Window" -> "Show" -> Click "Ant" on the Eclipse main menu-> Click "Ant", which is called out of the Ant view, right-click, right click on this view window. Select "Add Build File" in the pop-up menu, then load the "build.xml" file under the root of the HibernateBegin_2 project. The effect is as shown: This way, want to run a task, double-click the task in the Ant view. ● Generate user.java · Double-click the "Generate-Code" task, you should see the following output at the console: · If you don't see the "user.java" file in the src directory, select the src directory, then press the "F5" function button to refresh the src directory, you should see "User" under "javamxj.hibernate". Java. This file is "HBM2JAVA" generator is generated 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 with HBM.xml files * @author javamxj (sharing java happiness) * @Link Blog: htpp: //javamxj.mblogger.cn * htpp: //blog.9cbs.net/javamxj/ * * / Public Class User IMPLEments SERIALIZABLE { / ** Identifier Field * / Private string id; / ** Persistent Field * / PRIVATE STRING UserName; / ** Persistent Field * / PRIVATE STRING Password; / ** Full Constructionor * / Public user (string username, string password) { THIS.USERNAME = UserName; this.password = password; } / ** Default constructor * / Public user () { } Public string getId () { Return this.id; } Public void setid (String ID) { THIS.ID = ID; } / ** * @PARAM username * / Public string getUsername () { Return this.username; } Public void setusername (String username) { THIS.USERNAME = UserName; } Public string getpassword () { Return this.password; } Public void setpassword (string password) { this.password = password; } Public string toString () { Return New TostringBuilder (this) .append ("id", getId ()) .tostring (); } } You can look at the "User.hbm.xml" file to see what attributes are transformed into what code. ● Generate data table · Start MySQL, you should determine the HibernateTest database, but this time you don't need to create a data table. · Double-click the "SchemaExport" task, the console outputs as follows, pay attention to the automatically generated SQL statement: · At the same time, in the project root directory, a "schema-export.sql" file (if not, press the F5 key to refresh the directory), this file is set in "build.xml": Schema-export.sql Drop Table if Exists UserTable2 (id varchar (32) Not null, DDD VARCHAR (24) Not Null, Password VARCHAR (24) Not Null, Primary Key (ID)) · Switch to the database, will find that it has been automatically generated Dark data tablertable2: 5. Test procedure Ok, copy the Test.java file in the last article to the package "javamxj.hibernate", then right-click to run this file, you can see the data generated in the data table. summary Ok, let's take a look at the structure of the entire project, "User.java" and "Schema-Export.sql" two files are automatically generated, and the point to note is: Generate "Schema-Export.sql" file needs Call the "User.java" file, so "Generate-Code" task is to be executed before the "SchemaExport" task. It is best to use the XMLBUDDY plugin to edit the XML file, you can refer to: Regarding how to use HBM mapping files to generate ordinary Java objects and data sheets, you need to take a look at the reference document, but also exercise more, master in practice, advancement in practice. The next article talks with the XDoclet development of hibernate.