After several days of hard work and boredom, I finally used the myeclipse3.8 plug-in eclipse to successfully develop an application that uses hibernate for the persistent layer! It can be easily developed in JBX, but there is always a problem in eclipse. It may be that I am too stupid. Today I have finally fixed this basic problem. In order to avoid detours with beginners, in order to fulfill my commitment in the article eclipse3.0 + myeclipse3.8.1ga + tomcat5.0 + MySQL development JSP, I will record my operation steps and precautions as follows: (Note: for my development environment, see eclipse3.0 + myeclipse3.8.1ga + tomcat5.0 + MySQL development JSP.)
1. Create a Java project ---> hibtest;
2. Create two directories SRC and ADO for hibtest;
3. Right-click hibtest and add the hibernate attribute for the project. In the displayed dialog box, select/src as the directory by setting up personsessionfactary. Click "complete! Automatically generate personsessionfactary. Java and hibernate. cfg. xml;
4. window-> show view-> Other-> myeclipse _> dbbrowse, dbbrowse will be displayed in the main form, click New, the create new profile dialog box will appear, fill in your MySQL information, OK, right-click the profile you created and select open connection. the MySQL database and data table will appear, right-click Create hibernate mapping file, and a dialog box will be displayed to create the person class, abstractperson. java, person. java, person. HBM. XML (these files are all placed under/src );
5. Create test classes insert. Java and queryhib. Java in the/Dao directory.
Insert. Java
Package ADO;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;
Import SRC. person;
Import SRC. personsessionfactory;
/**
* @ Author Yang Qiang
*
*/
Public class insert {
Static session S = NULL;
Public static void main (string [] ARGs) throws exception {
Try {
S = personsessionfactory. currentsession ();
Person yuj = new person ();
Yuj. setname ("sfdhh ");
Yuj. setaddress ("sfhhf ");
Person X = new person ();
X. setname ("sfdhhfd ");
X. setaddress ("fshdfhd ");
// Persistence
S. Save (yuj );
S. Save (X );
S. Flush ();
System. Out. Print ("success ");
} Catch (hibernateexception e ){
System. Err. println ("hibernate exception" + E. getmessage ());
Throw new runtimeexception (E );
}
}
}
Queryhib. Java
Package ADO;
Import java. util. iterator;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. query;
Import net. SF. hibernate. Session;
Import SRC. personsessionfactory;
Import SRC. person;
/**
* @ Author Yang Qiang
*
*/
Public class queryhib {
Public iterator getperson ()
{
/*
* Use the connectionfactory to retrieve an open
* Hibernate session.
*
*/
Session session = NULL;
Try
{
Session = personsessionfactory. currentsession ();
/*
* Build hql (Hibernate query language) query to retrieve a list
* Of all the items currently stored by hibernate.
*/
Query query =
Session. createquery (
"Select person from person ");
Return query. iterate ();
}
Catch (hibernateexception E)
{
System. Err. println ("hibernate exception" + E. getmessage ());
Throw new runtimeexception (E );
}
/*
* Regardless of whether the above processing resulted in an exception
* Or proceeded normally, we want to close the hibernate session. When
* Closing the session, we must allow for the possibility of a hibernate
* Exception.
*
*/
}
Public static void main (string [] ARGs) throws exception {
Try {
Queryhib q = new queryhib ();
Iterator it = Q. getperson ();
While (it. hasnext ())
{
Person temp = (person) it. Next ();
System. Out. println (temp. GETID ());
System. Out. println (temp. getname ());
System. Out. println (temp. getaddress ());
}
}
Catch (exception ex)
{
System. Err. println ("hibernate exception" + ex. getmessage ());
Throw new runtimeexception (Ex );
}
}
}
6. Run insert. Java to insert the database;
7. Run queryhib to query database data;
Note: Is There a problem during running? It is because log4j is not configured. You can put log4j. properties under hibernate into this project. Everything is OK! No textures. Please forgive me!