. First of all, our preparation
1. Project
2. Database
Database execution commands. sql
1 /*2 Navicat MySQL Data Transfer3 4 Source server:mysql1235 Source Server version:506246 Source host:localhost:33067 Source Database:inverse8 9 Target Server Type:mysqlTen Target Server version:50624 One File encoding:65001 A - date:2016-06-30 15:46:41 - */ the -SET foreign_key_checks=0; - --- ---------------------------- +--Table structure forUsers --- ---------------------------- + DROP TABLE IF EXISTS ' users '; A CREATE TABLE ' users ' ( at' ID 'int(4) not NULL auto_increment, -' Name ' varchar ( -) DEFAULT NULL, -' Content ' varchar ( -) DEFAULT NULL, - PRIMARY KEY (' id ') -) Engine=innodb DEFAULT Charset=utf8;
We're ready now. With the above items and tables, we started the reverse engineering first step
1. Text does not matter, look at the picture
2. Next, we'll configure our Hibernate core Jar Package
Right click our project select myeclipse--"Project Facets-->install hibernate facet
3. Now we're back to starting with our own db.
4. Then that's it, let's take a look at our inverse entity class
5. This is good, thank you for watching, we learn together
Attach a copy of the configuration
Hibernate.cfg.xml
<?xml version='1.0'encoding='UTF-8'? ><! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-CONFIGURATION-3.0.DTD"><!--Generated by MyEclipse Hibernate Tools. -->"dialect">Org.hibernate.dialect.MySQLDialect</property> <property name="Connection.url">Jdbc:mysql://localhost:3306/test</property> <property name="Connection.username">root</property> <property name="Connection.password">root</property> <property name="Connection.driver_class">Com.mysql.jdbc.Driver</property> <property name="Myeclipse.connection.profile">test</property> <mapping resource="Com/hp/entity/users.hbm.xml"/> </session-factory>View CodeHibernatesessionfactory (Tool Class)
Package Com.hp.util;import Org.hibernate.hibernateexception;import org.hibernate.session;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.ServiceRegistryBuilder;/** * Configures and provides access to Hibernate sessions, tied to the ' current thread of execution. Follows the Thread Local Session * pattern, see {@linkhttp://hibernate.org/42.html }. */ Public classHibernatesessionfactory {/** location of hibernate.cfg.xml file. * location should is on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file was * in the default package. Use #setConfigFile () to update * The location of the configuration file for the current session. */ Private StaticFinal threadlocal<session> ThreadLocal =NewThreadlocal<session>(); Private Staticorg.hibernate.SessionFactory sessionfactory; Private StaticConfiguration Configuration =NewConfiguration (); Private StaticServiceregistry Serviceregistry; Static { Try{configuration.configure (); Serviceregistry=NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry (); Sessionfactory=configuration.buildsessionfactory (serviceregistry); } Catch(Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace (); } } Privatehibernatesessionfactory () {}/** * Returns the ThreadLocal Session instance. Lazy Initialize * The <code>SessionFactory</code> if needed. * * @return Session * @throws hibernateexception*/ Public Staticsession getsession () throws Hibernateexception {Session session= (Session) threadLocal.Get(); if(Session = =NULL|| !Session.isopen ()) { if(Sessionfactory = =NULL) {rebuildsessionfactory (); } Session= (Sessionfactory! =NULL) ?sessionfactory.opensession ():NULL; ThreadLocal.Set(session); } returnsession; } /** * Rebuild hibernate session Factory **/ Public Static voidrebuildsessionfactory () {Try{configuration.configure (); Serviceregistry=NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry (); Sessionfactory=configuration.buildsessionfactory (serviceregistry); } Catch(Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace (); } } /** * Close the single Hibernate session instance. * * @throws hibernateexception*/ Public Static voidclosesession () throws Hibernateexception {Session session= (Session) threadLocal.Get(); ThreadLocal.Set(NULL); if(Session! =NULL) {session.close (); } } /** * return session factory **/ Public Staticorg.hibernate.SessionFactory getsessionfactory () {returnsessionfactory; } /** * return hibernate configuration **/ Public StaticConfiguration getconfiguration () {returnconfiguration; }}
View CodeMyEclipse Database Reverse Hibernate tutorial