Hibernate: The framework of the persistence layer!
is an open source Object Relational Mapping Framework (ORM)! Before we accessed the database using jdbc!
A lightweight object encapsulation of JDBC! is a fully automatic ORM framework! (the underlying can automatically generate SQL statements)!
Use object-oriented thinking to manipulate the database!
Create a Java Project
Introduce the jar packages needed for hibernate and the drive packages needed to connect to the database
Put the jar package into the project and reference
Create the corresponding student class
/** * Student's entity class */public class Student { private Integer ID; Private Integer age; Private String name;//and database inconsistency (sname) public Integer getId () { return ID; } public void SetId (Integer id) { this.id = ID; } Public Integer Getage () { return age; } public void Setage (Integer age) { this.age = age; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public Student (integer ID, integer age, String name) { super (); This.id = ID; This.age = age; this.name = name; } Public Student () { super (); } @Override public String toString () { return ' Student [id= "+ ID +", age= "+ Age +", name= "+ name +"] "; }}
Create a student table in the corresponding database
Locate the corresponding mapping mapping file and place it under the same package as the entity class! and modify the contents of it
To modify the contents of a Student.hbm.xml file
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd /HIBERNATE-MAPPING-3.0.DTD "><!--mapping file M purpose; The entity class and tables in the database generate a relationship package: is the packet name of the entity class that we need to map class name: Corresponds to the name of the entity class (if the package attribute is omitted, the full class name must be written) Property,id name: corresponds to the attribute name in the entity class, and if it is consistent with the field in the database, you can omit column-->< Hibernate-mapping package= "Cn.bdqn.bean" > <class name= "Student" table= "Stu" > <id name= "id" > <!--primary key generation policy assigned: The generated value of the primary key is determined entirely by the user, regardless of the underlying database. You must specify a primary key value before calling Session.save (), otherwise an exception will be thrown! --<generator class= "Assigned"/> </id> <!--Configure other properties for mapping -- <property name= "Age"/> <property name= "name" column= "sname"/> </class></ Hibernate-mapping>
Locate the Hibernate core configuration file and modify the content
Modified Hibernate.cfg.xml File Contents
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">Create a corresponding test class
Package Cn.bdqn.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.junit.test;import cn.bdqn.bean.student;/** * * @author small tofu * After you, will thank you for your efforts now! Efforts! Insist! Don't give up! * * 1 Classes * 01 for Hibernate core. Class Configuration! Read the core configuration file! * 5 x Interface * 01. Sessionfactory: Responsible for initializing hibernate required Parameters! There's one in the program that's enough! Then we will write a singleton pattern! * 02. Session: Not our HttpSession (user session)! Our Hibernate session * is used to manipulate objects (increase and revise)! Create the object of the transaction! The session we are using now is not thread safe! * 03. Transaction: Do the operation of the transaction! * 04. Query:hibernate query in the interface (SQL, HQL) * 05. Criteria: similar to query! But did some encapsulation! */public class Studenttest {//New student information @Test public void AddstudenT () {/** * 01. Read the core profile because all the information we need is in this core configuration file (four elements of a database, a mapping file) * Configure () The bottom default goes to SRC and queries the Hiber Nate.cfg.xml file */Configuration Configuration = new configuration (). Configure (); 02. Create Sessionfactory Sessionfactory sessionfactory = Configuration.buildsessionfactory (); 03. Open Session Session session = Sessionfactory.opensession (); 04. Open transaction Transaction Transaction = Session.begintransaction (); 05. Create a Student object Student Student = new Student (400, 50, "small yellow"); 06. Persistent Operation Session.save (student); 07. Commits the transaction will produce the SQL statement transaction.commit (); 08. Close Session Session.close (); }//New student information @Test public void AddStudent2 () {/** * 01. Read the core profile because the information we need is in this core configuration file (the four elements that connect the database, Map file) * Configure () The lower-level default to SRC query the hibernate.cfg.xml file */Configuration Configuration = new Config Uration (). Configure (); 02. CreateSessionfactory sessionfactory sessionfactory = Configuration.buildsessionfactory (); 03. Open Session Session session = Sessionfactory.opensession (); 04. Open transaction Transaction Transaction = Session.begintransaction (); 05. Create a Student object Student student1 = new Student (50, 50, "small yellow 2"); Session.save (STUDENT1); Student Student2 = new Student (60, 50, "small yellow 2"); Session.save (Student2); Student Student3 = new Student (7, "Xiao Huang 2"); No manual assignment to the primary key will throw an exception session.save (STUDENT3); /** * 07. Submit Transaction * Student1 and Student2 no problem * But STUDENT3 error * then will the transaction be submitted? No! ACID
Consistency: atomicity: Isolation: Permanent:
* * If each save () generates an SQL statement, interact with the database! This database pressure is big! * How to lighten? * in Commit (), the previous SQL statements are sent to the database execution! * /Transaction.commit (); 08. Close Session Session.close (); }}
HBM2DDL: Attribute value
<!--whether to show the underlying generated SQL statement-- <property name= "Show_sql" >true</property> <!-- Format generated SQL statement-- <property name= "Format_sql" >true</property> <!-- Hbm2ddl 01.create: Every run will delete the last generated table! 02.update: No table will be created automatically, if any, add data! field is inconsistent, a new column of 03.validate is added to the database according to the value of the column property in the Hbm.xml file : The table is not created automatically and the data is added if the table exists! field is inconsistent and throws an exception, saying that there is no field 04.create-drop in the database : Every run will delete the last generated table! When the sessionfactory is closed, the tables in the database will also be deleted! -- <property name= "Hbm2ddl.auto" >update</property>
Summary
Hibernate: The framework of the persistence layer! is an open source Object Relational Mapping Framework (OMR)! Before we accessed the database, we used jdbc! to encapsulate JDBC in a Lightweight object package! is a fully automatic ORM framework! (the underlying can automatically generate SQL statements)! Use object-oriented thinking to manipulate the database! Student entity Class Student.hbm.xml mapping (Map file) Hibernate.cfg.xml: Is the core configuration file for the entire hibernate framework! 01. Managing mapping Files 02. Configuration of the entire Hibernate environment 03. Four languages of the four-factor SQL connected to the database: DDL: Data definition language create ALTER DROP TRUNCATEDML: Data manipulation Language Select Insert U Pdate Delete DCL: Data Control Language Grant REVOKETCL: Transaction Control Language savepoint rollback <!--hbm2ddl 01.create: Every run will delete the last generated table! 02.update: No table will be created automatically, if any, add data! field is inconsistent, a new column of 03.validate is added to the database according to the value of the column property in the Hbm.xml file: The table is not created automatically and the data is added if the table exists! field is inconsistent and throws an exception, saying that there is no field 04.create-drop in the database: Every run will delete the last generated table! When Sessionfactory is off, tables in the database are automatically deleted--<property name= "Hbm2ddl.auto" >update</property> Oracle database creation sequence create sequence Sq_student_idminvalue 1maxvalue 9999999999start with 10incrment by 1cache 20; <id name= "id" > <!--PRIMARY key generation strategy 01.assigned: The value generated by the primary key is determined by the user and is not related to the underlying database! You must assign a value to the primary key before calling Session.save (), otherwise an exception will be thrown!02.sequence:oracle supported primary key generation policy!hibernate will take the current sequence from the database the next value is assigned to the primary key! We don't need to assign a value to the primary key! <generator class= "sequence" > <param name= "sequence" >sq_student_id</param> </generator > 03.identity: Primary key for MySQL database auto-increment 04.increment: The default is to query the database for the maximum value of the primary key in the corresponding table (select MAX (ID) from Stu) Then give the value to the primary key! --<generator class= "increment"/> </id>
Hibernate02 setting up the environment