As an implementation of JPA, the JPA annotations are already at the heart of Hibernate, and Hibernate provides only a few additions, not two sets of annotations. Hibernate support for JPA is sufficient, and the use of Hibernate annotations suggests using JPA
XML version process:
New project, import hibernate package, database package, build database table, New model class, test class, new SRC under Hibernate.cfg.xml, model class under Student.hbm.xml
Annotation version Process:
New project, import hibernate package, database package, build database table, New Model class (add @entity (javax), and @id (primary key)), test class, New SRC under hibernate.cfg.xml (join map)
So annotation is relatively simple.
XML version specific process:
1. New->java Project:hibernate_0100_helloworld
2. Introduce all hibernate jar packages:
1. Preferrence->java->build path->user library-> New ("Hibernate"), Add JARs (Hibernate3, ANTLR, Commons-collections, dom4j, Javassist, Jta,slf4j-api, Slf4j-nop)
2. Right-click Project build-> Add Libraries->user library-> "Hibernate"
3. Introduce the database package build-> add external archieves-> mysql-connector ....
4. Creating a database table
Create DATABASE Hibernate;use hibernate;create table student (ID int primary key, name varchar (), age int);
5. New Class:student Package:com.bjsxt.hibernate.model
Package Com.bjsxt.hibernate.model;public class Student {private int id;private String name;private int age;public int Geti D () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
6. New Class:StudentTest.java Package:default
Import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Com.bjsxt.hibernate.model.student;public class Studenttest {public static void main (string[] args) throws exception{ Student s =new Student (); S.setid (2); S.setname ("Lisi"); S.setage (30); Configuration cfg=new configuration (); Sessionfactory sf=cfg.configure (). Buildsessionfactory (); The default is to find Hibernate.cfg.xml, and then produce a connection factory session session = sf.opensession (); Session.begintransaction (); Session.save (s); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}
7. Decentralization of Hibernate.cfg.xml in SRC:
<?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 ">8. Under model new Student.hbm.xml:
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
9. Run, Java application.
If you run an error, you usually need to add two packages: Cglib-nodep-2.1_3.jar, Commons-logging.jar
Annotation version specific process:
1. Add annotation related jar packages (hibernate-annotations, ejb3-persistence, hibernate-commons-annotations)
2. Create a teacher table:
Use Hibernate;create table teacher (ID int primary key, name varchar (), title varchar (10));
3. Create a new model class, Teacher.java:
Inside add @entity (javax), and @id (primary key)
Package Com.bjsxt.hibernate.model;import Javax.persistence.entity;import javax.persistence.Id; @Entitypublic class Teacher {private int id;private string name;private string title; @Idpublic int GetId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;}}
3. Establish a new test class Teachertest.java;
Import Javax.persistence.entity;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.cfg.configuration;import Com.bjsxt.hibernate.model.Teacher; @Entitypublic class Teachertest {public static void main (string[] args) throws Exception{teacher T =new Teacher () T.setid (3); T.setname ("ww"); T.settitle ("High"); Configuration cfg=new annotationconfiguration (); Sessionfactory sf=cfg.configure (). Buildsessionfactory (); The default is to find Hibernate.cfg.xml, and then produce a connection factory session session = sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}
4. Add this sentence to Hibernate-cfg.xml:
<mapping class= "Com.bjsxt.hibernate.model.Teacher"/>
Hibernate---First HelloWorld program (XML version, annotation version)