- What is 1.Hibernate?
Hibernate is an open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases at will using object programming thinking.
Object Relationship Mapping abbreviation ORM (relational Mapping), because most of our current programming language is object-oriented programming, the use of databases are mostly relational database, the data can be used in the form of objects in the program, and the program Some objects (data Objects) have to be persisted in the database, so there is an ORM, said more popular point: ORM is the data in the database and the object in the program to convert each other!
Today's ORM framework can be said to be rich:
- Hibernate, open source ORM framework, widely used (most widely used)
- IBATIS, open source, maintained by ASF, and have. NET porting (more widely used)
- TopLink, developed by Oracle
- Cayenne,apache,java Open Source
- QUICKDB Orm, open source ORM Framework (GNU LGPL)
- Java Data Objects (JDO)
- Java Persistence API (JPA)
- JPOX, open source JDO 2 reference implementation
- Object Relational Bridge (Apache OJB), a Java objects-relational mapper
- Openjpa,apache, open source, support for JPA APIs
2.Hibernate's relationship with JDBCHibernate encapsulates JDBC, persisting objects in memory, and synchronizing to the database.
1. Regarding encapsulation, I have to emphasize two points:
(1). Hide as many things as possible. Provide a simple interface to the outside.
(2) Hide all the attributes.
Of course, we would be better off if we could do a single principle.
2. About the concept of persistence
Persistence definition: A mechanism for converting program data between persistent and transient states.
This is about hibernate. Some of the states in this case are: instantaneous state, persistent state, off-tube state
- Instantaneous state (Transient): An object that opens up space in memory. If user User=new User (), the user object is instantaneous.
- Persistent State (persistent): data, such as objects in memory, is saved to a storage device that can be permanently saved (such as a disk). Can be in the form of files, databases, and XML.
- Off-State (Detached): When the session associated with a persistent object is closed, the persisted object is turned into a de-tube object. When the off-tube object is re-associated to the session, it is again transformed into a persistent object.
Here I would like to summarize a bit about the instantaneous state and persistent state: Instantaneous states are in memory, if the computer shuts down, and then start, before the memory of the object is no longer, if the persistent state, the data object stored on the disk, when necessary or can be loaded into memory, will not be affected by the computer startup!
3.SessionThe session interface is the most important interface that hibernate provides to the program to manipulate the database.
- Transient state (Transient): not associated with session
- Persistent State (persistent): associated with session, not close
- Off-tube status (Detached): After Session.close
A detailed approach to 4.Session
1. Save
SVAE: Inserts the database immediately and returns the primary key
Persist: Do not immediately (delay) Insert database, no return value
2. Get
Load: After loading the object, changes to the object are not immediately flushed to the DB and must be flush to db
Ex:user user=session.load (user.class,2);
User.setname (' GT ');
User.flush (); (Lazy Loading)
Get: Changes to the object are immediately flushed to the DB after the object is loaded
3. Update
Update: Persistent object, updating
saveorupdate: Contains the Save () and update () functions, and calls the Save () method if the passed parameter is a temporary object (not saved), or the update () method if the passed argument is a free object
Merge: Object is not persisted, only changes to the managed object are updated to DB
4. Delete
Delete: delete records from the database that correspond to Java objects
5. Cleaning up
Flush: Synchronizing the cache to DB
Clear: Clear the cache size of session (should be considered when updating batches)
5. Mapping relationships
See the individual articles in the blog
Introduction to Hibernate (Session, several states, methods ...) such as