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:
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
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.
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.