1.1 , Hibernate Quick Previews
Hibernate is currently a very popular ORM Framework, Object Relation Model, through the ORM framework we can simply use the object to operate it can easily implement the operation of the database. 3.6.8
Entry:
1. Importing Hibernate jar Packages
-hibernate-->lib-->required
-hibernate-->hiberate.jar
-hibernate-->lib-->jpa
-log4j
-mysql-connector
2. Create the appropriate business object (User)
3. Create Hibernate configuration file
-src-->hibernate.cfg.xml, make the corresponding database connection settings in this configuration file
4, for the corresponding business objects to create HBM configuration file, in this file to describe the mapping relationship with the database, if created for the user object in the package containing the user object to create a User.hbm.xml file
5. Add hbm files to the CFG file
6. Write code
7, in order to easily query the corresponding SQL statement, you need to copy log4j.properties to the project
1.2 , using Hibernate Implement CRUD
1. Write the same hibernateutil create a singleton sessionfactory and session uniformly in this class
2, Write Add method
3. Write the Update method
4. Write the Load method
5. Write the Delete method
6. Write the list method
1.3 , Hibernate the State
1. Transient (instantaneous state)
No ID, no session Management
2. Persistent (persistent state)
When the save,update is executed, the object will be managed by the session and then called persistent
The persisted state object is an ID.
3. Detached (Offline status)
ID, but not managed by the session, (session closed, create an object, set the corresponding ID)
4, the transformation of the object state
5. Several special cases
5.1. If the save operation is performed on the offline object, the update will not be executed and the INSERT,
5.2, the corresponding instantaneous object, if the execution update will throw an exception (id not found)
5.3, Saveorupdate
Special Note: Saveorupdate This method is generally not used because we are fully controlling the state of the data object during the development process
5.4, a session can only have a copy of the same ID persisted state object
1.4 , lazy loading
There are two ways to get an object: Get and load
• Lazy loading is not supported for Get methods (Lazyload (lazy Loading))
• Supports lazy loading for load, deferred loading refers to the execution of the load, and does not directly emit SQL statements to the database to fetch data, only after using the object's non-id attribute will not call SQL to the database to fetch data, this is an optimized strategy
• Problems that may occur with the use of load
• Some special cases
Special case for Get
Special Case for Load
Delay loading can be canceled by setting up HBM files (this is not normally done)
1.5 , ID Build Policy
ID is the identity that each object must have, and the general case is a field with no semantics as the ID
Generator means that you can set the build policy for IDs, for MySQL: auto_increment, for Oracle: Use sequence to generate an auto-increment sequence.
Native indicates that the database will be selected automatically, if it is MySQL choose Auto_increment, if it is Oracle Select sequence
For native, the ID must be of type int
There is another generation strategy: UUID (if using the UUID primary key must be a varchar type)
You can set the build policy to assigned,assigned to indicate that the primary key is specified by the user (infrequently used)
1. Hibernate basics