Eclipse Quick Start With hibernate-3. Use XDoclet for development

Source: Internet
Author: User
Tags jboss
This article is the first two articles, "Eclipse Quick Start With hibernate -- 1. getting started instance and eclipse Quick Start hibernate -- 2. using the continuation of hBM ing file development, we mainly talk about how to use the xdoclethbm ing file to generate HBM ing files and data tables. You can refer to the hibernate Development Guide written in Hibernate and Xia Xin in the XDoclet document. Similarly, this article does not talk much about theory, but provides a complete example to illustrate it. For more information about the configuration, see the previous two articles. 1. Create a project· Create a Java project: hibernatebegin_3. Select "create a separate source folder and Output Folder" and add "User library": hibernate. 2. File user. Java· Create a new class with the package name javamxj. Hibernate and Class Name: user. Then add the variable to the generated code and then use "generate getter and setter". The specific method is the same as "Eclipse Quick Start hibernate -- 1. edit user in the Getting Started instance article. java is the same. · Add the hibernatedoclet tag. For tips on how to use JBoss-ide To add XDoclet tags, see eclipse Quick Start to EJB -- 1. the section about JBoss-ide in lomboz + JBoss-ide configuration 2 is the code after hibernatedoclet is added:

User. JavaWw w. China it power. co43apgsn

/** Use hibernatedoclet to develop a simple hibernate instance * creation date: 2005-3-31 * @ author javamxj (Happy Java sharing) * @ link blog: htpp: // javamxj.mblogger.cn * htpp: // blog.csdn.net/javamxj/ */package javamxj. hibernate;/*** @ hibernate. class table = "usertable3" */public class user {private int ID; private string username; private string password;/*** @ hibernate. id * column = "ID" * Generator-class = "HiLo" */Public int GETID () {return ID;} public void setid (int id) {This. id = ID;}/*** @ hibernate. property * length = "24" * Not-null = "true" */Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;}/*** @ hibernate. property * column = "username" * length = "24" * Not-null = "true" */Public String GetUserName () {return username;} public void setusername (string username) {This. username = username ;}}

· Add the class tag "@ hibernate. Class table =" usertable3 "to generate the database table usertable3. · "@ Hibernate. ID" is used to generate a primary key. Note that the Hilo (high/low bit) generator is used here. An additional database table is required to save the primary key to generate a historical state. · "@ Hibernate. Property" describes the ing between attributes in pojo and database table fields. ● Update XDoclet-hibernate-Module
Copy the xdoclet-hibernate-module-1.2.2.jar in xdoclet1.2.2 to the JBossIDE-1.4.1-e30/Eclipse/plugins/org. JBoss. IDE. eclipse. XDoclet. core_1.4.1 directory, and delete the xdoclet-hibernate-module-1.2.1.jar files under its directory, then in the eclipse interface, window-> preferences-> JBoss-ide-> XDoclet-> code assist: click Refresh XDoclet data on the right to complete the update. To replace other modules, follow these steps. 3. Project Structure· Copy "build. xml" in the previous article to the project root directory, and copy the configuration file "hibernate. cfg. xml" to the src directory. 4. Run the task· Double-click the "generate-HBM" task and press the "F5" function key to refresh the package "javamxj. hibernate". You can see the "user. HBM. xml" under this package ". The file is as follows:

User. HBM. xmlWw w. China it power. co43apgsn

<? XML version = "1.0" encoding = "GBK"?>

<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 2.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd>

<Hibernate-Mapping
>
<Class
Name = "javamxj. hibernate. User"
Table = "usertable3"
Dynamic-update = "false"
Dynamic-insert = "false"
Select-before-update = "false"
Optimistic-lock = "version"
>

<ID
Name = "ID"
Column = "ID"
Type = "int"
>
<Generator class = "HiLo">
<! --
To add non XDoclet generator parameters, create a file named
Hibernate-generator-params-User.xml
Containing the additional parameters and place it in your merge dir.
-->
</Generator>
</ID>

<Property
Name = "password"
Type = "Java. Lang. String"
Update = "true"
Insert = "true"
Access = "property"
Column = "password"
Length = "24"
Not-null = "true"
/>

<Property
Name = "username"
Type = "Java. Lang. String"
Update = "true"
Insert = "true"
Access = "property"
Column = "User Name"
Length = "24"
Not-null = "true"
/>

<! --
To add non XDoclet property mappings, create a file named
Hibernate-properties-User.xml
Containing the additional properties and place it in your merge dir.
-->

</Class>

</Hibernate-mapping>

Generate data table· Start MySQL and check whether the database contains hibernatetest. However, no data table is required this time. Double-click the schemaexport task and refresh the project root directory to find the generated schema-export. SQL file.
Schema-export. SQL
Drop table if exists usertable3
Drop table if exists hibernate_unique_key
Create Table usertable3 (
Id integer not null,
Password varchar (24) not null,
User name varchar (24) not null,
Primary Key (ID)
)
Create Table hibernate_unique_key (
Next_hi integer
)
Insert into hibernate_unique_key values (0)
· Switch to the database and you will find that the usertable3 and hibernate_unique_key data tables are automatically generated:

5. Test ProcedureOK. copy the Java file to the package "javamxj. hibernate, and right-click to run the file to see the data generated in the data table (the data in the figure is generated after four consecutive runs ). 6. log4j Log4j is an open source project. It allows developers to control log output at any interval. It allows you to flexibly set runtime settings by setting an external configuration file. The library files running log4j have been put in the previously created hibernate library folder. You only need to find the etc directory in the downloaded hibernate 2.1.8 compressed file and copy the log4j. the properties file is in the src directory of the project. Run test. java. You can see that the output Statement on the console is no different from that of log4j. Open log4j. properties, find the "log4j.logger.net. SF. hibernate = Info" statement, and change it to "log4j.logger.net. SF. hibernate = WarnRun test. Java again. We can see that there are only two output statements on the console: 21:03:55, 687 warn configurator: 125-No configuration found. Grouping ing ehcache from ehcache-failsafe.xml found in the classpath: jar: file:/D:/Java/hibernate/lib/ehcache-0.9.jar! Ehcache-failsafe.xml
Hibernate: insert into usertable3 (password, username, ID) values (?, ?, ?)
Warning information indicates that the cache configuration file is not found and will be used later. Okay, it's so easy to use hibernate with log4j. The use of log4j is relatively simple, and there are a lot of online materials. You can Google it yourself. Summary: The development of Hibernate is quite flexible and can be developed in multiple ways. · Only HBM ing files: ing files --- hbm2java ---- Java --- schemaexport ---- Data Tables · only Java: Java --- XDoclet --- HBM ---- schemaexport ---- Data Tables · What if only data tables are available? You can use middlegen: data table --- middlegen --- HBM ---- hbm2java ---- java. For more information about using middlegen to develop hibernate, see the hibernate Development Guide compiled by Xia Xin. · There are many Eclipse plug-ins related to hibernate. You can refer to working with hibernate in eclipse.
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.