hibernate-4.3.5 installation Configuration

Source: Internet
Author: User
Tags generator

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 Class

package 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. Testing

Package 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 ();}}


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.