1. Development environment preparation
Eclipse3.0.1
Download: http://www.eclipse.org/
Eclipse Plugin: Hibernate Synchronizer
Download: http://sourceforge.net/project/showfiles.php? Group_id = 99370
Hibernate-2.1.8
Download: http:/internap.dl.sourceforge.net/sourceforge/hibernate/Hibernate-2.1.8.zip
Mys QL 4.1
Download: http://www.mysql.com
2. Demonstrate a simple example
2.1 Creating a table content table content under the Test database in the mysql database, the script is as follows:
Create Table Content (FID INT (11) Not Null Auto_Increment, Ftitle Varchar (100) Not Null Default ', Fcontent Text, Primary Key (FID));
2.2 Detection if the Hibernate plugin is successfully installed, enter "preferences" under the menu "Window", as shown below:
2.3 Create a Java project with Eclipse, as shown below:
Select "Next>" until completion.
One thing to note is that after creating a project, you have to join the Hibernate library and mysql driver into the project's libraries.
2.4 Join the Hibernate configuration file "Hibernate.cfg.xml"
Select the item, right click, "new" à "taher" into the following interface,
Select "Hibernate Configuration File" à "Next"
Select the database type, Database URL, UserName, and Password, OK, file "hibernate.cfg.xml" system automatically generated.
2.5 Join "* .hbm" configuration file
Also, choose the item, right click, "new" à "ot" enters, but this time you have to choose "hibernate mapping file", click "Next" to enter the following interface
Note that if the database has a password, you must first enter the password and click the "Refresh" button, and the table in the data is displayed in the Tables box. Select a table, such as Content, click the "Finish" button.
2.6 Generate related Java code according to Content.hbm, select the content.hbm right button, and the following interface:
2.7 One way to simply add a few lines of code, enter the system generated package DAO, as "com.hibernate.example.dao" in the example, open the file contentdao.java, the code modified as follows:
0: / * 1: * Create Date 2004-12-10 2: * @Author Oscar Lao 3: * Copyright 2005 SWS 4: * / 5: 6: package.com.hibernate.example.dao; 7: 8: import Org .apache.log4j.logger; 9:10: import com.hib.hibernate.example.content; 11: import com.hibernate.example.base.basecontentdao; 12: 13: / ** 14: * @Author oscar lao15: * qq : 6306506816: * Email: e-silver@163.com17: * ClassDesc: Hibernate Example18: ** / 19: public class ContentDAO extends BaseContentDAO {20: private static final Logger logger = Logger.getLogger (ContentDAO.class); 21: 22: Public static void main (String [] args) {23: if (logger.IndebugeNabled ()) {24: logger.debug ("Program is running ..."); 25:} 26: 27: try {28 : _Rootdao.initialize (); 29: 30: Contentdao Cond = New Contentdao (); 31: Content Cont = New Content (); 32: for (INT i = 1; I <100; i ) {33: Cont.setFID (New Integer (I)); 34: Cont.setftitle ("Title" i); 35: Cont.setfcontent ("Content" i); 36: Cond.Save (cont); 37:} 38:} catch (Exception E) {39: logger.error ("Exception in Run ...", e) 40:} 41: 42: IF (logger.IndebugeNabled ()) {43: logger.debug ("running. "); 44:} 45:} 46:} 3. Appreciation results
If all configurations are correct, the ContentDao class should be able to run, after running, the program will insert 100 records to Table Content. good luck! !
4. There is a mistake when I test, and now it is also posted, I hope to help everyone:
4.1 If there is an abnormality when running
Could Not Find UserTransaction In JNDI JAVAX.NAMING.NOINITIALCONTEXTEXCEPTION: ...
Solve: Put the configuration file "hibernate.cfg.xml"
Java: Comp / Usertransaction
Property>
The attribute comment for the two lines to set the JTA control transaction is OK, because JTA control transactions have not been used in the program.
4.2 If there is an abnormality when running
Error Parsing XML: /Hibernate.cfg.xml (33) THE Content of Element Type "Session-Factory" Is Incomplete, IT Must Match "(Property *, Mapping , (Class-Cache | Collection-Cache | JCS-CACHE | jcs-collection-cache) * "".
SOLUTION: Plus between