Hibernate knowledge point sorting

Source: Internet
Author: User

Hibernate knowledge point sorting
1. Requirements for persistence objects 1) provide a non-argument constructor 2) provide an identity attribute, such as id, which is usually mapped to the primary key field of the database table. 3) provides get and set methods for fields in the persistence class. Note: not all fields do this. For fields that do not provide the get or set method, they must be processed in the ing file (*. hbm. xml. For example, if the get and set methods are not set for a property description, the ing is: <property name = "description" type = "string" column = "descri" access = "field"> </property> 4) the class cannot be defined as 5 of final) to put the persistence class instance in the set, rewrite the equals () and hashCode () Methods of the persistence class. 2. Differences between the basic data type and the packaging type. 1) the hibernate ing type corresponding to the basic data type and the packaging type is the same (the ing is the same); 2) However, the basic data type can be directly operated, cannot express null, and the default value is 0; 3) the default value of the packaging class is null. For the default value, you need to use the packaging class. For example, the Student class has an int type score attribute, indicating the Student's exam score. the score attribute of the int type cannot express such business requirements: * If the score attribute is null, the score of the student is Unknown, may score 100 or 0, but the score is not yet known * If the scope attribute is 0, it indicates that the student's test score is 0. * In the preceding case, the packaging type must be used. 3. The persistence class property policy propertye (default value) indicates that hibernate uses getXXX and setXXX to define class properties. Recommended. Improves the transparency of the domain (object) model. Field hibernate uses the java reflection mechanism to directly render class attributes. You can set this access policy for attributes without the get and set methods. Noop maps attributes that do not exist in the Java persistence class. It is mainly used in HQL (test using the query interface and using hql statements). If a Column exists in the database but does not exist in the object.

1 <! -- This property has get and set methods in the Customer class --> 2 <property name = "name" column = "name" type = "string"/> 3 4 <! -- The get and set methods of this property do not exist in the Customer class --> 5 <property name = "name" column = "name" type = "string" access = "field"/> 6 7 <! -- This attribute does not exist in the Customer class, but this field exists in the database. 8. Use noop for processing. Ignore this field when querying --> 9 <property name = "name" column = "name" type = "string" access = "noop"/>

 

4. the Java language uses the memory address (=) or equals () method to distinguish objects. In Hibernate, Object Identifiers (OID) are used to distinguish objects. 5. Primary Key 1) the required condition cannot be null. Unique. It cannot be repeated. It will never change. 2) primary Key classification business primary Key (Natural primary Key): In the database table, fields with business logic meanings are used as the primary Key, which is called "Natural primary Key (Natural Key )". Logical primary key (proxy primary key): In a database table, a field irrelevant to the logic information in the current table is used as its primary key, which is called the "proxy primary key ". Composite primary key (Federated primary key): uses a combination of two or more fields as the primary key. Natural primary key: a field with business meaning is called a natural primary key as the primary key. Proxy primary key: a field that does not have business meaning. This field is generally named "id ". (Recommended) 3) generation policy Increment increment identifier generator Hibernate assigns an incremental value to the proxy primary key. Hibernate will first read the maximum value of the primary key in the table. When a record is inserted into the table, the increment is 1 based on max (id. Applicability: a is suitable for all database systems because the increment survival identifier mechanism does not depend on the underlying database system. B is applicable to scenarios where only a single Hibernate application process accesses the same database, and there may be problems in the case of multithreading. The c OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running. The Identity identity identifier generator is used by the underlying database to generate the identifier. It requires the underlying database to define the primary key as the Automatically increasing field type. Applicability: a. Because the identity generation identifier mechanism depends on the underlying database system, the underlying database system must support automatic field growth. Databases that support automatic field growth include DB2, Mysql, MSSQLServer, and Sybase. The B OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running. The Sequence sequence identifier generator uses the Sequence provided by the underlying database to generate the identifier. when Hibernate persists a News object, it first obtains a unique ID number from the news_seq sequence of the underlying database, and then uses it as the primary key value range: because the sequence generation mechanism of sequence depends on the sequence of the underlying database system, the underlying database system must support the sequence. Databases supporting sequences include DB2 Oracle. The B OID must be of the long, int, or short type. If the OID is defined as a byte type, an exception is thrown during running. The Hilo hilo identifier generator is generated by Hibernate according to a high/low algorithm *. It obtains the high value from the fields of a specific table in the database. By default, use the next_hi field of the hibernate_unique_key table. When Hibernate persists a News object, Hibernate is responsible for generating the primary key value. when the hilo identifier generator generates the identifier, it needs to read and modify the NEXT_VALUE value in the HI_TABLE table. applicability: a. Because the hilo survival identifier mechanism does not depend on the underlying database system, it must be of the long, int or short type for All database systems, if the OID is defined as a byte type, an exception will be thrown at runtime. The Native native identifier generator selects to use the identity, sequence, or hilo identifier generator based on the underlying database's support for automatically generated identifiers. applicability: native can automatically select an appropriate identifier generator based on the type of the underlying database system. Therefore, native is suitable for cross-database platform development. The class B OID must be of the long, int, or short type, if the OID It is defined as a byte type and will throw an exception during runtime. UUID applies to the proxy primary key. Hibernate uses the 128-bit UUID algorithm to generate identifiers. This algorithm can generate unique string identifiers in the network environment. This policy is not popular because string-type primary keys occupy more database space than integer-type primary keys. Applicability: a does not depend on the underlying database system, so it is suitable for all database systems. B OID must be of the String type Assigned, indicating that the primary key value is manually specified. It is applicable to natural primary keys.

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.