This article will use Maven3, Hibernate3.6, oracle10g integration, as an example.
Environment Checklist:
1.maven3.0.5
2.hibernate3.6.5 Final
3.jdk1.7.0.11
4.oracle10g
first create a table Bduser
CREATE TABLE DBUSER ( user_id number (5) NOT NULL, username VARCHAR2 (), created_by VARCHAR2 (+), created_date date)
two. Create a Web project with Maven3, project name: Maven-hibernate-demo
To create a project reference: Maven3 (iii) creating the first Web project with MAVEN (1)
three. Add Hibernate and Oracle dependencies, Pom.xml as follows
Hibernate some dependent packages must be added
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.lei.demo</groupId> <artifactId> Maven-hibernate-demo</artifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT< /version> <name>maven-hibernate-demo maven webapp</name> <url>http://maven.apache.org</url > <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit< ;/artifactid> <version>4.8.2</version> <scope>test</scope> </dependency> <!--Oracle Jdbc Driver---<dependency> <groupId>com.oracle</groupId> <artifa Ctid>ojdbc14</artifactid> <version>10.2.0.4.0</version> </dependency> <!--Hibernate configuration--<dependency> <groupid>org.hibernate</group id> <artifactId>hibernate-core</artifactId> <version>3.6.5.Final</version> < ;/dependency> <dependency> <groupId>javassist</groupId> <artifactid>javassist& lt;/artifactid> <version>3.12.0.GA</version> </dependency> <dependency> < ;groupid>antlr</groupid> <artifactId>antlr</artifactId> <version>2.7.6</versio n> </dependency> <dependency> <groupId>commons-collections</groupId> <ar tifactid>commons-collections</artifactid> <version>3.1</version> </dependency> < ;d ependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <v Ersion>1.6.1</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <ar tifactid>jta</artifactid> <version>1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <VERSION&G t;1.6.1</version> </dependency> <dependency> <groupid>org.hibernate.javax.persistence </groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.final</v ersion> </dependency> </dependencies> <build> <finalname>maven-hibernate-demo</final Name> </build></project>
four. Creating hibernate map files and model classes
1. Create a Dbuser.hbm.xml mapping file
Under Path Src/main/resources/com/sulei/demo
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2013-10-22 15:28:34 by Hibernate Tools 3.4.0.CR1-->< hibernate-mapping> <class name= "Com.sulei.demo.Dbuser" table= "Dbuser" schema= "Gzip_base" > <id name= " UserId "type=" int "> <column name=" user_id "precision=" 5 "scale=" 0 "/> <generator class=" Assigned "/> </id> <property name=" username "type=" string "> <column name=" USERN AME "length="/> </property> <property name= "CreatedBy" type= "string" > <colu MN name= "created_by" length= "/> </property> <property name=" createddate "type=" Date "> <column name= "Created_date" length= "7"/> </property> </CLASS></HIBERNATE-MAPPING&G t;
2. Create the entity class Dbuser
Under Path Src/main/java/com/sulei/demo
Package com.sulei.demo;//Generated 2013-10-22 15:28:34 by Hibernate Tools 3.4.0.cr1import java.util.date;/** * Dbuser Gen Erated by Hbm2java */public class Dbuser implements java.io.Serializable {private int userId; Private String username; Private String CreatedBy; Private Date createddate; Public Dbuser () {} public Dbuser (int userId) {This.userid = UserId; } public Dbuser (int userId, string username, string createdby, Date createddate) {This.userid = user Id; This.username = Username; This.createdby = CreatedBy; This.createddate = CreatedDate; } public int getUserId () {return this.userid; } public void Setuserid (int userId) {This.userid = UserId; } public String GetUserName () {return this.username; } public void Setusername (String username) {this.username = username; } public String Getcreatedby () {return this.createdby; } public void Setcreatedby (String createdby) {this.createdby = CreatedBy; Public Date Getcreateddate () {return this.createddate; } public void Setcreateddate (Date createddate) {this.createddate = CreatedDate; }}
You can use Hibernate tools to reverse engineer an automatic sound field code, see another article below:
Generating Hibernate mapping mapping files with Hibernate tools
Five. Create Hibernate.cfg.xml
Create Hibernate profile "Hibernate.cfg.xml" put him under the resources directory: "Src/main/resources/hibernate.cfg.xml" fill in the database information.
Add the Map file, "DBUser.hbm.xml".
Hibernate.cfg.xml as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
Six. Create Session workshopCreate the "Hibernateutil.java" class to manage the Hibernate Session. Path: "Src/main/java/com/sulei/util/hibernateutil.java"
Package Com.sulei.util;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class Hibernateutil { private static final Sessionfactory sessionfactory = Buildsessionfactory (); private static Sessionfactory Buildsessionfactory () { try { //Create the sessionfactory from hibernate.cfg.xml< C4/>return new Configuration (). Configure (). Buildsessionfactory (); } catch (Throwable ex) { //Make sure your log the exception, as it might be swallowed System.err.println ("Initial Se Ssionfactory creation failed. "+ ex); throw new Exceptionininitializererror (ex); } } public static Sessionfactory Getsessionfactory () {
SYSTEM.OUT.PRINTLN ("test----1"); return sessionfactory; } public static void Shutdown () { //Close caches and connection pools getsessionfactory (). Close (); }}
six. Code testingCreate a App.java to insert a record into the table.
Package Com.sulei.test;import Java.util.date;import Org.hibernate.session;import com.sulei.util.HibernateUtil; Import Com.sulei.demo.dbuser;public class App {public static void Main (string[] args) { System.out.println (" Maven3 + Hibernate + oracle10g "); Session session = Hibernateutil.getsessionfactory (). Opensession (); Session.begintransaction (); Dbuser user = new Dbuser (); User.setuserid (+); User.setusername ("Leioolei"); User.setcreatedby ("system"); User.setcreateddate (New Date ()); Session.save (user); Session.gettransaction (). commit (); }}
The project is well organized and the catalogue structure is shown
Right-click on the App.java and the result
Test success!!!
Maven3 Distance (v) creating Hibernate projects with Maven