Using Eclipse+hibernate Synchronizer (plugin) Development Example (original)

Source: Internet
Author: User
Tags interface mysql net mysql database
Original
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. Demo a simple example

2.1 In the MySQL database, under the test database to create table content table contents, the script is as follows:

CREATE TABLE Content (

FID Int (one) not NULL auto_increment,

Ftitle varchar not NULL default ',

Fcontent Text,

PRIMARY KEY (FID)

);









2.2 Detect whether the Hibernate plug-in successfully installed, enter the menu "window" under the "Preferences", there are the following figure shown:













2.3 Create a Java project with Eclipse, as follows:











Choose "Next>" until finished.

One thing to note is that after the project is created, the Hibernate library and MySQL driver are added to the project's libraries.

2.4 Add hibernate config file "Hibernate.cfg.xml"

Select the item, click the right button, and "New" à "other" into the following interface,





Select "Hibernate Configuration File" à "Next"



Select the database type, DB URL, username, and Password,ok, and the file "Hibernate.cfg.xml" system is automatically generated.

2.5 adding "*.HBM" configuration file

Also, select the item, click Right, "New" à "other" entry, but this time to select "Hibernate Mapping File", click "Next" into the following interface





Note that if the database has a password, you must first enter the password and then click the "Refresh" button, and the table in the data will be displayed in the Tables box. Select a table, such as content, after the "Finish" button.

2.6 Generate relevant Java code according to CONTENT.HBM, select CONTENT.HBM Right button, operate the following interface:



2.7 to simply add a few lines of code can be, into the system generated package DAO, such as the "Com.hibernate.example.dao" in the example, open the file Contentdao.java, the code changes as follows:

0:/*



1: * Date of Creation 2004-12-10



2: * @author Oscar Lao



3: * CopyRight S.W.S



4: */



5:



6:package Com.hibernate.example.dao;



7:



8:import Org.apache.log4j.Logger;



9:



10:import com.hibernate.example.Content;



11:import Com.hibernate.example.base.BaseContentDAO;



12:



13:/**



* @author Oscar Lao



: * qq:63065068



*: * email:e-silver@163.com



*: * classdesc:hibernate Example



18: **/



19:public class Contentdao extends Basecontentdao {



20:private static final Logger Logger = Logger.getlogger (Contentdao.class);



21st:



22:public static void Main (string[] args) {



23:if (logger.isdebugenabled ()) {



24:logger.debug ("program is running ...");



25:}



26:



27:try {



: _rootdao.initialize ();



29:



30:contentdao cond = new Contentdao ();



31:content cont = new Content ();



32:for (int i = 1; i < i++) {



33:cont.setfid (New Integer (i));



34:cont.setftitle ("Title" +i);



35:cont.setfcontent ("Content" +i);



36:cond.save (cont);



37:}



The catch (Exception e) {



39:logger.error ("Exception at run time ...", e);



40:}



41:



42:if (logger.isdebugenabled ()) {



43:logger.debug ("Run complete.") ");



44:}



45:}



46:}









3. Appreciation of the results

If all the configurations are correct, the Contentdao class should be ready to run and the program will insert 100 records into the table content. Good luck!!







4. In my test when the following error occurred, and now posted out, I hope to help you:







4.1 If the following exception occurs at run time







Could not find usertransaction in JNDI javax.naming.NoInitialContextException: ...







Resolve: Put the configuration file "Hibernate.cfg.xml" in the







<property name= "Hibernate.transaction.factory_class" >







Net.sf.hibernate.transaction.JTATransactionFactory







</property>







<property name= "JTA. UserTransaction ">







Java:comp/usertransaction







</property>







Two lines set the JTA to control the properties of the transaction, because the JTA control transaction is not used in the program.







4.2 If the following exception occurs at run time







Error parsing XML:/hibernate.cfg.xml The content of element type "Session-factory" is incomplete, it must match "(pro perty*,mapping+, (class-cache|collection-cache|jcs-class-cache|jcs-collection-cache) *) ".

Workaround: Add between <session-factory> and </session-factory> in the profile "Hibernate.cfg.xml"







<mapping resource= "CONTENT.HBM"/>























For the first time write about Hibernate, if there is any wrong place, please criticize me.







Finally, I wish you all success!! ^_^








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.