Hibernate Learning (1), hibernate Learning

Source: Internet
Author: User

Hibernate Learning (1), hibernate Learning
1. Introduction to Hibernate 1. Introduction to Hibernate

Hibernate is an open-source object relationship ing (ORM) framework. It implements lightweight object encapsulation for JDBC and establishes ing relationships between POJO and database tables, it is a fully automated orm framework. hibernate can automatically generate and execute SQL statements, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate is a bridge between traditional Java objects and database servers. It is used to process Objects Based on the O/R ing mechanism and mode.

2. Advantages and Disadvantages of Hibernate

Advantages and disadvantages are relative, so we need to compare the advantages and disadvantages of one thing, and we want to compare the native JDBC of data operations:

1. Advantages and Disadvantages of JDBC

Essence: Process conversion between Java objects and relational database tables.

Advantages:

Operate on the lowest level of the database, with the highest performance (you need to have relevant experience and be a database master ).

Disadvantages:

1. Complex use (too many repeated code ).

2. It is very troublesome to port the database, and there are many changes. The primary key generation method is different (mysql uses auto-increment and oracle uses sequence). The paging SQL statements are also different (mysql uses limit, oracle uses ROWNUM ).

3. The performance is optimized by yourself, and no data cache is provided.

4. SQL statement-oriented operations are not object-oriented.

2. Advantages and Disadvantages of hibernate

Essence: It processes conversions between Java objects and relational database tables, but encapsulates JDBC again.

Advantages:

1. Simple operations for programmers, simple code session. save (user );

2. Direct object-oriented operations.

3. provides world-class data caching (almost all ORM framework caches are learning Hibernate); level-1 cache, level-2 cache, and query cache.

4. The database is highly portable and rarely modified. A dialect interface is extracted from various databases. Different databases implement a dialect interface. If the database is changed, the dialect implementation must be modified to drive the jar file, connect to the database.

Disadvantages:

1. cannot interfere with the generation of SQL statements; session. get (User. class, id); all fields in the t_user table are queried by default. select user0 _ is automatically generated _. id, user0 _. name, user0 _. age from t_user user0 _ where user0 _. id =?

2. In a project, if the optimization requirements for SQL statements are relatively high, it is not suitable to use hibernate (however, you can use Hibernate to support native SQL ).

3. If a table contains hundreds of millions of data records, it is not applicable to hibernate, but jdbc (you can use database read/write splitting and database/table sharding solutions ).

2. Start Hibernate 1. Step 1. Copy the jar package

2. Create a pojo

Product class:

 1 package com.hibernate.pojo; 2  3 /** 4  * @author zt1994 2018/3/6 14:16 5  */ 6 public class Product { 7     private Integer id; 8     private String name; 9     private float price;10 11     public Integer getId() {12         return id;13     }14 15     public void setId(Integer id) {16         this.id = id;17     }18 19     public String getName() {20         return name;21     }22 23     public void setName(String name) {24         this.name = name;25     }26 27     public float getPrice() {28         return price;29     }30 31     public void setPrice(float price) {32         this.price = price;33     }34 }
3. Create a ing File

Product. hbm. xml ing file:

1 <! DOCTYPE hibernate-mapping PUBLIC 2 "-// Hibernate/Hibernate DTD ing DTD // EN" 3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 4 5 <! -- Ing file --> 6 4. Create a core hibernate configuration file

Hibernate. cfg. xml configuration file

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE hibernate-configuration PUBLIC 3 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <! -- Hibernate core configuration file --> 7 5. Edit dao implementation class

Dao interface:

1 package com. hibernate. dao; 2 3 import com. hibernate. pojo. product; 4 5 import java. util. list; 6 7 public interface IProductDao {8 9/** 10 * add Product 11 * @ param product12 * @ return13 */14 void addProduct (product Product); 15}

Dao implementation:

1 package com. hibernate. dao. impl; 2 3 import com. hibernate. dao. IProductDao; 4 import com. hibernate. pojo. product; 5 import org. hibernate. query; 6 import org. hibernate. session; 7 import org. hibernate. sessionFactory; 8 import org. hibernate. transaction; 9 import org. hibernate. cfg. configuration; 10 11 import java. util. list; 12 13/** 14 * @ author zt1994 2018/3/6 4015 */16 public class ProductDaoImpl implements IProductDao {17 18 @ Override19 public void addProduct (Product product) {20/1. read and parse the Configuration file 21 configuration Configuration = new Configuration (); 22 // 2. load the configuration file. If you do not configure to load the default configuration file hibernate. cfg. xml23 configuration. configure ("hibernate. cfg. xml "); 24 // 3. generate session factory 25 SessionFactory sessionFactory = configuration. buildSessionFactory (); 26 // 4. obtain session27 Session session = sessionFactory. openSession (); 28 // 5. enable Transaction 29 transaction Transaction = session. getTransaction (); 30 transaction. begin (); 31 // 6. operate the CRUD32 session. save (product); 33 transaction. commit (); 34 // 7. shut down resource 35 session. close (); 36 sessionFactory. close (); 37} 38}

6. Test

1 package com. hibernate. test; 2 3 import com. hibernate. dao. IProductDao; 4 import com. hibernate. dao. impl. productDaoImpl; 5 import com. hibernate. pojo. product; 6 import org. junit. test; 7 8/** 9 * @ author zt1994 2018/3/6 14: 4710 */11 public class TestProductDao {12 private IProductDao productDao = new ProductDaoImpl (); 13 14/** 15 * test adding Product 16 */17 @ Test18 public void testAddProduct () {19 product Product = new product (); 20 Product. setName ("test"); 21 product. setPrice (2222); 22 productDao. addProduct (product); 23} 24}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.