1. The use of Annotation is mainly divided into 2 steps:
1.1 Add the appropriate jar package:
Hibernate-annotations.jar// Annotation Core Pack
Ejb3-persistence.jar// implementation of Annotation compliant with JPA standards
Hibernate-commons-annotations.jar//Use when launching
1.2 annotating classes and attributes in the model using the @ annotation form
2. new hibernate_annotation, Engineering structure directory such as:
3. Add the appropriate jar package:
2.1 Add hibernate to the corresponding jar package.
2.2 Add the MySQL-driven jar package.
2.3 Add a jar package that supports annotations.
2.4 such as:
4. Code:
4.1 Student.java
Package Com.hibernate.model;import Javax.persistence.entity;import javax.persistence.Id, @Entitypublic class Student {private int id;private string name;private int age;public Student (int id, string name, int age) {super (); this.id = Id;thi S.name = Name;this.age = age;} @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 int getage () {return age;} public void Setage (int.) {this.age = age;}}
4.2 Hibernate.cfg.xml
<?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 ">4.3 studenttest.java
Package Com.hibernate;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.cfg.configuration;import Com.hibernate.model.student;public class Studenttest {public static void main (string[] args) {Student s = new Student (1, "s 1 ", +);//cfd.configure (), configure () do not write parameter default find in src directory hibernate.cfg.xmlConfiguration cfd = new Annotationconfiguration (); Find configuration file//buildsessionfactory () produces a sessionfactory factory sessionfactory SF = Cfd.configure (). Buildsessionfactory (); Session session = Sf.opensession (); Session.begintransaction (); Session.save (s); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}
5. Part of the explanation:
5.1 Add @Entity to the class name of model Student.java and introduce import javax.persistence.Entity; Enables Annotation to recognize that this is an entity class.
5.2 Add the @Id on the Model Student.java GetId () and introduce the import javax.persistence.Id; make Annotation is able to identify and indicate that this is a primary key.
5.3 Configure <mapping class= "Com.hibernate.model.Student"/> in Hibernate.cfg.xml.
5.5 using Annotation to manipulate database steps:
5.5.1 New A annotationconfiguration () object and calls the Configure () Find profile hibernate.cfg.xml.
5.5.2 Gets the configuration file and creates the Sessionfactory through Buildsessionfactory ().
5.5.3 creates a Session by Sessionfactory's Opensession ().
5.5.4 opens a thing through the BeginTransaction () of the Session.
5.5.5 calls the Session's Save (), update (), delete () and other methods to perform database operations.
5.5.6 gets what is currently being manipulated through the Session's Gettransaction () and updates the data to the database by taking the commit () of the thing.
5.5.7 finally turn off Session, Sessionfactory.
6. Source code Download
End
Hibernate Learning Notes 03--hibernate + MySQL + Annotation