Reprinted---Hibernate learning one

1. What is Hibernate?

  The persistence layer framework for lightweight JavaEE applications is a complete ORM framework. (After saying this sentence, there must be many people who are confused, let me explain one by one)

  Persistence: Save the data we want to save to the hard disk, that is, the disk of our computer. Why is it called persistence? It means that the data can be stored for a long time, so it is called persistence. Now the implementation process of persistence is mostly done through Various relational databases are completed, so we often say that saving data in the database is actually the database that helps us save the data to the hard disk.

          

 

  Persistence layer: Now that you know what persistence is, then the persistence layer should have some ideas. Here, the database is regarded as a part of the memory, and we treat it as saving the data in the database and saving it to the hard disk, so The layer that operates the database or deals with the database is the persistence layer. For example, if we know the three-layer architecture before, isn't there a layer dedicated to dealing with the database called the persistence layer?

  ORM: Object Relational Mapping, object relational mapping, this is an idea, model, or specification. The records in the relational database are mapped into object instances in the programming language, and then the operation of the database is achieved by manipulating the objects. If there is no ORM idea, we have directly manipulated the record fields in the database to achieve the purpose of storing data.    

  Persistent class: As explained above, the persistent class can save the class to the database and get the class from the database. This is called the persistent class, which is the POJO class mentioned below.

  Persistent object: The instance object of the persistent class can be saved to the database or retrieved from the database.

    The concept of JPA: Java Persistence API java persistence API, which is the specification of java persistence, ORM is defined in this JPA, it also stipulates many other specifications, JPA maintains a Persistence Context (persistence context), which is This persistence context is coming. Those ORM frameworks must be designed according to the JPA specification, so each ORM framework also has such a persistence context. The general content of the persistence context: 1. ORM metadata, JPA supports two forms of annotation (annotation) or xml to describe object/relational mapping 2. Entity operation API, which implements CRUD operations on entity objects 3. Query language, which stipulates object-oriented The query language JPQL (javaPersistence Query Language)

    

      Knowing some general terms, let's take a look at the status of hibernate in a project

      Brief Architecture

                

      The brief hibernate architecture is shown in the figure above. As we said, hibernate is between the Application and the Database, so we use hibernate to implement the operation of the Database.

      We configure xxx.hbm.xml: the purpose is to associate hibernate with our application

        We configure hibernate.cfg.xml: database related services, such as: username and password, etc. There are their own hibernate services.

      Hibernate comprehensive solution architecture

                

 

 

Second, what is the role of hibernate?

         

    1. Through hibernate, complete the mapping relationship between POJO classes and database tables

    2. Through hibernate, only need to operate the object, hibernate will help us generate database statements to operate the database, we don't have to care about the following statements.

    3. Probably the two mentioned above, is to allow users to add, delete, and modify objects to achieve the operation of adding, deleting, and modifying data in the database table.

 

 

 

Third, the use of hibernate needs to import the jar package and the meaning of the jar package?

      Although I don't know how the jar package is implemented, I still have that curiosity. I must know what this jar package is for, otherwise I'm like a fool who knows how to copy and paste. I believe that after learning this hibernate, I will Know what's in the jar package.

      The mapping relationship between pojo classes and database tables is reflected through the xxx.hbm.xml configuration file.

            

      Basically, you only need to import the above 11 jar packages. If you need other functions, you may import additional jar packages

        antlr.jar: Another Tool for Language Recognition can construct a language recognizer, which is used when parsing HQL (later, hibernate Query language).

        commons-collections.jar: is the enhanced version of collections. More powerful collection classes than java.util.*

        dom4j.jar: used for parsing xml,

        hibernate-jpa.jar uses the jar package that hibernate depends on, jpa is a specification, and hibernate is an implementation of it.

        hibernate3.jar: the core jar package of hibernate

        javassist.jar: Manipulate bytecode files, related to cglib (cglib should be a kind of dynamic proxy, with jdk dynamic proxy)

        jta.jar:java transaction api, which is related to transactions

        log4j.jar: log4j log

        mysql-connector.jar: mysql connection driver package

        slf4j-api.jar: The standard interface for integrating other logs, that is, if you want to integrate the jar packages of other logs, you must conform to this standard

        slf4j-log4j.jar: used to integrate log4j and the specification interface, so that log4j conforms to the specification, so that it can be used

 

  

Fourth, how to realize the mapping relationship between POJO classes and database tables, the xxx.hbm.xml configuration file is very powerful

         Because of this configuration file, the pojo class has the ability to operate.

              

    

    1. Dtd file: found in org.hibernate/hibernate-mapping-3.0.dtd under hibernate3.jar.

    2. Basically, you only need to add <hibernate-mapping></hibernate-mapping>, and write code under this pair of tags to map.

    3. For example, User.java User.hbm.xml

        User.java

              

      User.hbm.xml

              

      Primary key production strategy:

            1, increment: the primary key grows automatically, managed by hibernate

                Note: If the database is also set to grow automatically, there will be a primary key conflict problem

            2. identity: managed and generated by the underlying database, not managed by hibernate

                That is to say, how to set the primary key of the underlying database.

                Note: mysql, sql server can, oracle can not

            3. sequence: identifier generator, which is the underlying database to manage the generation, and uses the sequence provided by the underlying database to generate the identifier, which is not managed by hibernate

                Note: mysql does not support sequences oracle supports

            4. Native: The underlying database decides what strategy to use, regardless of hibernate

                Note: mysql automatically selects identity, oracle automatically selects sequence

            5. uuid: Randomly generate 32-bit different strings.

      Primary key is divided into natural primary key and surrogate primary key

            1 Natural primary key: that is, the primary key that has specific meaning in the business,

            2 The surrogate primary key, which is the 5 types we mentioned above, has no meaning, but the identification primary key is unique.

      

      For some other configuration information, and what you don't understand, you can refer to the hibernate api manual. It's very detailed inside.

 

Five, hibernate configuration, hibernate.cfg.xml

      It is not enough to have the mapping file xxx.hbm.xml, because hibernate needs to connect to the database, so where should these operations be placed, a public configuration file is extracted. hibernate.cfg.xml is such a public configuration file, load The database connection information and the loading of various mapping files are actually extracted, because there are many mapping files, and each mapping file needs to connect to the database and other operations, so the common operations are extracted for a long time to form hibernate.cfg .xml.

            

 

      1. DDL strategy:

            1. Create, create a table. When starting, drop first and then create a table. (Testers use it to test data, first clear the previous old table, and create a new table)

            2. create-drop: also means to create, after creation, drop it. (test program is correct)

            3. update: Check whether the class and table are consistent, if inconsistency will be updated, and the table will be updated to the same as the class

            4. validate: Check whether the table and class are consistent at startup. If they are inconsistent, an exception will be reported.

              Generally, update and validate are commonly used.

 

Sixth, with xxx.hbm.xml and hibernate.cfg.xml, you can use the function of hibernate.

      

    

    Note: There is an additional date attribute in the User class in the screenshot in this chapter, which is to explain the type attribute in xxx.hbm.xml. Nothing else works. can be ignored.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063886&siteId=291194637