At first, according to official documents, always error, seemingly official documents wrong, looked up a lot of information, a comprehensive collation of a feasible solution, as follows:
0.1 Package Structure
Test.demo
Test.domain//entity class
Test.util//Tool class
0.2-Lead jar package
hibernate-4.3.5 all of the required packages
Optional all of the C3P0 in the package
Download slf4j, import Slf4j-api.jar and Slf4j-log4j.jar
Download log4j, import Log4j.jar
Import Mysql-connector-java.jar
The relationship between log4j and slf4j: http://blog.csdn.net/lifuxiangcaohui/article/details/7278595
apache-log4j-1.2.17 http://pan.baidu.com/share/link?shareid=121565833&uk=2047106924
slf4j-1.7.7 http://pan.baidu.com/share/link?shareid=123250651&uk=2047106924
1.POJO as follows
Package Test.domain;public class Message {private Long id;private String text;private message nextmessage;public message ( String text) {this.text = text;} Public Long GetId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetText () {return text;} public void SetText (String text) {this.text = text;} Public Message Getnextmessage () {return nextmessage;} public void Setnextmessage (Message nextmessage) {this.nextmessage = Nextmessage;}}
2. mapping file corresponding to it Message.hbm.xml, note, to be put together with Pojo
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
3.hibernate.cfg.xml, hibernate configuration file, placed in src root directory
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd ">
4.hibernateutil.java Tool Classpackage Test.util;import Org.hibernate.sessionfactory;import Org.hibernate.boot.registry.StandardServiceRegistryBuilder; Import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;public class HibernateUtil { private static final Sessionfactory sessionfactory = buildsessionfactory ();p rivate static sessionfactory Buildsessionfactory () {try {configuration configuration = new configuration (). Configure (); Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties () ). build (); Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry); return SessionFactory;} catch (Throwable ex) {System.err.println ("Initial sessionfactory creation failed." + ex); throw new Exceptionininitializererror (ex);}} public static Sessionfactory Getsessionfactory () {return sessionfactory;}}
5. TestingPackage Test.demo;import Org.hibernate.session;import Test.domain.message;import test.util.hibernateutil;public Class HelloWorld {public static void main (string[] args) {Session session = Hibernateutil.getsessionfactory (). Opensession (); Session.begintransaction (); Message message = new Message ("Hello,world"); session.save (message); Session.gettransaction (). commit ();}}