This article is the last two articles. "
Eclipse quickly rapidly hibernate - 1. Getting Started Example "and"
Eclipse quickly came to Hibernate - 2. Using HBM mapping file development, the main point of how to generate HBM mapping files and data sheets using the XDocLethBM mapping file. You can refer to the section of Hibernate in the xdoclet documentation ""
Hibernate Development Guide. Similarly, this article did not talk about theory, just give a complete example. Related configuration, please refer to
Two articles.
Create a project
· New Java project: hibernatebegin_3, pay attention to "Create a separate source folder and output folder" while adding "user library": hibernate.
2. File User.java
· Create a category, package name: javamxj.hibernate, Classification: User. Then add variables in the generated code, and use "Generate Getter and Setter", the specific method "
Eclipse Quick Pickup Hibernate - 1. Getting Started Examples The Edit User.java in the article.
· Add Hibernatedoclet tag, how to use JBoss-IDE to add xDoclet tag tags
Eclipse Quick Pickup EJB - 1. Lomboz JBoss-IDE Configuration 2 "About JBoss-Ide, this is the code after adding Hibernatedoclet:
User.java
/ *
* Develop a simple Hibernate instance using Hibernatedoclet
* Create Date 2005-31
* @author javamxj (sharing java happiness)
* @Link Blog: htpp: //javamxj.mblogger.cn
* htpp: //blog.9cbs.net/javamxj/
* /
Package javamxj.hibernate;
/ **
* @ hibernate.class table = "useertable3"
* /
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 = "User Name"
* Length = "24"
* not-null = "true"
* /
Public string getUsername () {
Return UserName;
}
Public void setusername (string username) {this.username = username
}
}
· Add class tag "@ hibernate.class table =" usertable3 "to generate a table UserTable3 of the database.
· "@ Hibernate.id" is used to generate primary keys. Note that this is an HILO (high low) generator, and additional database tables will be used to save the primary key generation history.
· "@ Hibernate.property" describes the mapping relationship between the properties and the database table field in POJO.
● Update xdoclet-hibernate-module
• Copy xDoclet-hibernate-module-1.2.jar in XDoclet1.2.2 to 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 file under its directory, then in the Eclipse interface, window -> preferences -> jboss-ide -> xdoclet -> code assist: Click on "Refresh XDoclet Data" The update can be completed. If you want to replace other modules, the steps are similar.
3. Project structure
· Copy "Build.xml" in the last article to the root root directory, configure the file "Hibernate.cfg.xml" to copy to the src directory, then the project structure is shown in the figure:
4. Run the task
· Double-click "Generate-HBM" task, then press the "F5" function button to refresh the package "javamxj.hibernate", you should see "User.hbm.xml" under this package. The document is as follows:
User.hbm.xml
xml
Version = "1.0"
eNCoding =
"GBK"
?>
DOCTYPE
Hibernate-maping
Public
"- // Hibernate / Hibernate mapping DTD 2.0 // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
Hibernate-maping
>
<
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.xmlcontaining the additional parameters and place it in your mergever.
->
Generator
>
id
>
<
Property
Name =
"
Password
"
TYPE =
"
Java.lang.string
"
Update =
"
True
"
INSERT =
"
True
"
Access =
"
Property
"
COLUMN =
"
Password
"
Length =
"
twenty four
"
NOT-NULL =
"
True
"
/>
<
Property
Name =
"
Username
"
TYPE =
"
Java.lang.string
"
Update =
"
True
"
INSERT =
"
True
"
Access =
"
Property
"
COLUMN =
"
username
"
Length =
"
twenty four
"
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-maping
>
●
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, then refresh the project root directory, will find the generated "schema-export.sql" file.
Schema-export.sql
drop table if exists UserTable3drop table if exists hibernate_unique_keycreate table UserTable3 (ID integer not null, password varchar (24) not null, username 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, it will find that the data tableterTable3 and Hibernate_unique_Key have been automatically generated:
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 (the data in the figure is running 4 times continuously produced).
6. log4j
Log4j is an open source project. It allows the developer to control the output of the log in any interval. It achieves flexible settings at runtime by setting a configuration file outside.
Running the log4j library files have been put in the previously established Hibernate library folder, you only find the downloaded Hibernate 2.1.8 compressed file in the ETC directory, copy the log4j.properties file to the SRC directory in the project. . Run Test.java, you can see that the output statement of the console seems to have no difference between the previous log4j.
Open log4j.properties now, find the "log4j.logger.net.sf.hibernate = info" statement, change it to "log4j.logger.net.sf.hibernate =
"Warn", run Test.java again, you can see that there are two output statements in the console.
:
21: 03: 55,687 WARN Configurator: 125 - No Configuration Found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar: file: / d: /java/hibernate/lib/ehcache-0.9.jar! / Ehcache- Failsafe.xmlhibernate: INSERT INTO UserTable3 (Password, User Name, ID) VALUES (?,?,?)
Warning information means that the cache configuration file is not found, and then talk about this later.
Ok, Hibernate combines the use of Log4j is so simple.
Regarding the use of log4j, it is relatively simple, there are many online information, you can google.
summary:
Hibernate's development is quite flexible and can be developed in a variety of ways.
· Only HBM mapping file: mapping file --- hbm2java ---- Java --- SchemaExport ---- Data Sheet
· Only Java: java --- xdoclet --- HBM ---- SchemaExport ---- Data Sheet
· If there is only a data sheet? Middlegen can be used:
Data table --- Middlegen --- HBM ---- HBM2JAVA ---- Java
About using MiddleGen development hibernate, you can refer to Xia Wei's ""
Hibernate Development Guide.
· As for the Hibernate's Eclipse plugin, there are many, you can take a look "
Working with hibernate in eclipse.