Hibernate -- Overview and HelloWorld, hibernatehelloworld
1. Hibernate Overview
1. Hibernate is a persistence framework.
(1) In a narrow sense, "persistence" only refers to permanent storage of objects in the memory to the database on the hard disk.
(2) In a broad sense, "persistence" includes database-related operations. For example, CRUD.
2. Hibernate is An ORM framework.
ORM: object relationship ing. O object-oriented: Class, object, and attribute. R orientation: tables, rows (records), and columns (fields ). M ing.
ORM: a row (record) in a relational database is mapped to an object. A programmer can convert operations on the database to operations on the object.
In Hibernate, the object relationship ing file is used to describe the ing between objects and table records.
3. Hibernate configuration file: hibernate. cfg. xml. Configure the database connection, some information about Hibernate, and object link ing.
4. Entity. hbm. xml configuration file: Object link ing file.
5. Session interface: Session is a single-threaded object for interaction between applications and databases. It is the center of Hibernate operations. Session has a level-1 cache, which is equivalent to Connection in JDBC.
6. SessionFactory interface: generate the Session factory according to the configuration.
7. Transaction: Transaction, which can be enabled through Session.
Ii. HelloWorld
(1) Create a New hibernate. cfg. xml file under Intellij Idea and create an object and Entity. hbm. xml file based on the table have been discussed in the previous article.
(2) The generated hibernate. cfg. xml file.
<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD/EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
(2) created news table.
(3) generated News entity and News. hbm. xml file.
/*** @ Author solverpeng * @ create 2016-09-28-*/public class News {private int id; private String title; private String author; private Date date; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getAuthor () {return author;} public void setAuthor (String author) {this. author = author;} public Date getDate () {return date;} public void setDate (Date date) {this. date = date ;}@ Override public boolean equals (Object o) {if (this = o) {return true;} if (o = null | getClass ()! = O. getClass () {return false;} News news = (News) o; if (id! = News. id) {return false;} if (title! = Null?! Title. equals (news. title): news. title! = Null) {return false;} if (author! = Null?! Author. equals (news. author): news. author! = Null) {return false;} if (date! = Null?! Date. equals (news. date): news. date! = Null) {return false;} return true;} @ Override public int hashCode () {int result = id; result = 31 * result + (title! = Null? Title. hashCode (): 0); result = 31 * result + (author! = Null? Author. hashCode (): 0); result = 31 * result + (date! = Null? Date. hashCode (): 0); return result ;}}News. java <? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> (4) test
- Get SessionFactory object
- Get the Session object through SessionFactory
- Enable transaction through Session
- Perform database operations
- Commit transactions
- Close Resources
@ Testpublic void test () {// 1. create SessionFactory sessionFactory = null; Configuration configuration = new Configuration (). configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (configuration. getProperties ()). buildServiceRegistry (); sessionFactory = configuration. buildSessionFactory (serviceRegistry); // 2. create Session session = sessionFactory. openSession (); // 3. enable Transaction transaction Transaction = session. beginTransaction (); // 4. execute database operation News news = new News (); news. setTitle ("Title"); news. setAuthor ("tom"); news. setDate (new Date (new java. util. date (). getTime (); session. save (news); // 5. commit transaction. commit (); // 6. disable Session. close (); // 7. disable SessionFactory sessionFactory. close ();}
Note:
① The SessionFactory object is obtained through the Configuration object. The configure () method of Configuration reads the hibernate. cfg. xml file in the class path by default.
The configuration read from hibernate. cfg. xml is registered with ServiceRegistry. The generated SessionFactory object is generated according to the hibernate. cfg. xml configuration.
② Enable a transaction: The beginTransaction () method of the Session.
③ Pay attention to resource shutdown.
④ In fact, in the container environment, we do not need to pay attention to the acquisition of SessionFactory and close the resource.
Result:
Iii. Summary
In the first part of the Hibernate overview, I decided to learn about Hibernate from the following aspects: the Hibernate configuration file, the object link ing file (the main content is the link ), session API, cache, and transaction control.
From the second part of Helloworld, I have experienced the convenience of Hibernate. I have mentioned how to add configurations in the previous article. I will introduce the specific configurations in later articles.