1. Requirements for persistent objects
1) provides a parameterless constructor
2) provides an identity attribute, such as an ID, that is typically mapped to a primary key field of a database table.
3) provides a get, set method for the fields of the persisted class.
Note: But not necessarily all fields do this, for fields that do not provide a get, set method, are processed in the mapping file (*.hbm.xml).
For example, if a property description does not have a GET, set method mapped to:
<property name= "description" type= "string" column= "Descri" access= "field" ></property>
4) class cannot be defined as final
5) To put an instance of a persisted class into set, override the Equals (), Hashcode () method of the persisted class
2. Differences between the use of basic data types and packaging types
1) The same type of hibernate mapping as the basic data type and package type (mapping is the same);
2) But the basic type can be directly calculated, unable to express NULL, the default is 0;
3) wrapper class defaults to NULL, for which the default value has actual business meaning to use wrapper class
For example: The Student class has an int type of score attribute that represents the student's Test score. The score attribute of type int cannot express such business requirements:
* If the attribute of the score is null, it means that the student's score is unknown, it is possible to get 100 points, it is possible to get 0 points, but temporarily do not know the results
* If the scope attribute is 0, the student's test score is 0.
* The packing type must be used in the above case
3. Policies for persisting class properties
Propertye (default value):
Indicates that Hibernate accesses class properties through GetXXX and setxxx. Recommended use. Improves transparency of the domain (object) model.
Field
Hibernate accesses the class properties directly through the Java reflection mechanism. You can set this access policy for properties that do not have a get and set method.
NoOp
It maps a property that does not exist in the Java Persistence class, that is, used primarily for HQL (test with query interface, using the HQL statement), when there is a column in the database, and the entity does not exist.
1 <!--This property has a get and set method in the Customer class -2 < Propertyname= "Name"column= "Name"type= "string"/>3 4 <!--This property does not exist in the customer class for the Get and set methods -5 < Propertyname= "Name"column= "Name"type= "string"Access= "Field" />6 7 <!--The property does not exist in the customer class, but the field exists in the database. 8 use noop processing to ignore this field when querying -9 < Propertyname= "Name"column= "Name"type= "string"Access= "NoOp"/>
Property Policy Examples
4. The Java language distinguishes between different objects by memory address (= =) or Equals () method
Hibernate uses object identifiers (OIDs) to differentiate objects
5. PRIMARY KEY
1) required conditions
cannot be null.
Unique, cannot be duplicated.
Will never change.
2) Primary Key classification
Business Primary KEY (natural primary key): The field with the business logic meaning in the database table as the primary key, called the "Natural primary key (Natural key)".
Logical PRIMARY KEY (proxy primary key): a field in a database table that is independent of the logical information in the current table as its primary key, called the surrogate primary key.
Composite PRIMARY KEY (Federated primary key): A primary key by a combination of two or more fields.
Natural PRIMARY key: The field with business meaning as the primary key is called the natural primary key.
Surrogate PRIMARY key: A field that does not have a business meaning, which is generally named "ID." Recommended
3) Build Policy
Increment
The increment identifier generator is incremented by Hibernate to assign a value to the surrogate primary key
Hibernate first reads the maximum value of the primary key in the table, and when the record is inserted into the table, it increments on the basis of Max (ID), which is incremented by 1.
Scope of application:
A because the increment survival identifier mechanism does not depend on the underlying database system , it is suitable for all database systems.
b applies to situations where only a single Hibernate application process accesses the same database, and in multithreaded situations there is a problem.
The C OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception is thrown at run time.
Identity
The identity identifier generator is responsible for generating identifiers by the underlying database, which requires the underlying database to define the primary key as the autogrow field type.
Scope of application:
A because the identity generation identifier mechanism relies on the underlying database system , it is required that the underlying database system must support the Autogrow field type. Databases that support autogrow field types include: DB2, Mysql, MSSQLServer, Sybase, and so on.
The B OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception is thrown at run time.
Sequence
The sequence identifier generator uses the sequence provided by the underlying database to generate identifiers.
Hibernate when persisting a News object, first obtains a unique identification number from the NEWS_SEQ sequence of the underlying database, and then takes it as the primary key value
Scope of application:
A because the mechanism of sequence generates identifiers relies on the sequence of underlying database systems, it is required that the underlying database system must support sequences. Databases that support sequences include: DB2 Oracle, and so on.
The B OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception is thrown at run time.
Hilo
The Hilo identifier generator is generated by Hibernate according to a high/low algorithm *, which gets the high value from the field of a particular table in the database, by default, in the Next_hi field of the Hibernate_unique_key table.
Hibernate is responsible for generating the primary key value when a News object is persisted by hibernate. When the Hilo identifier generator generates an identifier, it needs to read and modify the Next_value value in the Hi_table table.
Scope of application:
A because the Hilo survival identifier mechanism is not dependent on the underlying database system, it is suitable for all database systems
The B OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
Native
The native identifier generator chooses to use the identity, sequence, or Hilo identifier generator, depending on the underlying database's ability to automatically generate identifiers.
Scope of application:
A because native can automatically select the appropriate identifier generator based on the type of underlying database system, it is well suited for cross-database platform development
The B OID must be a long, int, or short type, and if the OID is defined as a byte type, an exception will be thrown at run time
Uuid
applies to the proxy primary key. Hibernate uses a 128-bit UUID algorithm to generate identifiers. This algorithm can generate a unique string identifier in the network environment, which is not popular because the primary key of the string type takes up more database space than the primary key of the integer type.
Scope of application:
A does not depend on the underlying database system, so it is suitable for all database systems
b OID must be of type string
Assigned
represents a value that specifies a primary key manually, for a natural primary key
Hibernate Knowledge points Carding