1, Configuration/sessionfactory/session
The configuration instance represents a complete set of Java types in an application to SQL database mappings. Configuration is used to build a (immutable (immutable)) sessionfactory.
Sessionfactory is thread-safe and creates a high cost.
Session is non thread safe and lightweight. A session corresponds to a JDBC connection,
The session's connection () gets the corresponding database connection connection object for the sessions.
The function of the session is to manipulate objects, which have a mapping relationship with the database tables.
The object of the session operation is stateful and is divided into three categories:
Free State (transient): not persisted, not associated with any session, there is no corresponding record in the database table.
Persistent State (persistent): associated with a session, corresponding to a record in the database table.
Free State (detached): Has been persisted, but is not currently associated with any session, there is a record in the database table, there is no known now.
Instances of the Free State can be persisted by invoking the Save (), persist (), or Saveorupdate () method. A persisted instance can become a free State by calling delete (). Instances that are obtained through the get () or load () methods are persisted. An instance of a free state can be persisted by calling update (), 0saveOrUpdate (), lock (), or replicate (). An instance of a free or free State can be a new persisted instance by invoking the merge () method.
2, Session of Save ()/persist ()/update ()/saveorupdate ()/merge ()/delete () method
The Save () method saves the specified object and inserts a single piece of data into the table;
The persist () method saves the specified object, inserts a data into the table, and I haven't found anything special about it and the Save method.
The replicate () method persists the entity of a given Free State (Transient) entirely using the value of each property of the given object (including the identity ID), which is violent, and it also needs to specify a storage mode (there are four save policies to choose from).
The update () method updates the specified object, updating a data in the table;
The Saveorupdate () method receives an entity object and determines whether a save or update operation exists based on the ID of the entity object, so that the save and update methods are unified;
The merge () method copies the state of the given object to a persisted object with the same identity.
The Delete () method deletes the specified object and deletes a single piece of data from the table;
Special note: In order to use the Saveorupdate () method, you can determine what to do by setting the <id> tag's unsaved-value= "null" when the mapping file is defined: When the ID property equals the value of Unsaved-value (null), the save operation should be performed or the update operation should be performed. After this setting, you can use the Saveorupdate () method to unify the methods of saving and updating.
<id name= "id" column= "id" type= "java.lang.Integer" unsaved-value= "null" >
<generator class= "native"/>
</id>
There are four values that Unsaved-value can set:
Any: Always store
None: Always update
Store (Preset) when null:id is null
stored when valid:id is null or a specified value
3. Session's Get ()/load () method
The get () method always queries the entity object and returns NULL when it does not exist;
The load () method is also to get an entity object, which is not present when the null pointer exception is thrown.
4, Session of Clear ()/evict () method
The clear () method clears all entities (including various State) objects in the session-level cache to free memory.
The evict () method clears the specified entity (including various State) objects in the session-level cache.
Of course, after the session is closed, these caches do not exist and wait for the JVM to recycle.
5, session of the Flush () method
Flush () forces the entity objects in the session cache to be persisted. Clear () or evict () is generally invoked to quickly save and release valuable memory resources.
6, the session of the Commit ()/rollback () method
The commit () method is used to commit transactions on the session, otherwise the work unit does not have an impact on the database. If an exception is performed (that is, a commit () fails), the previous operation is canceled, and rollback () can undo the previous action.
7. The close ()/isopen ()/isconnected ()/reconnect () Method of Session
The close () method closes the database connection for the session, and its associated object lifecycle ends.
The IsOpen () method checks whether the session is still open, and if the session has been disconnected, you can use reconnect (Connection Connection) to reconnect the session to a JDBC connection.
The IsConnected () method checks whether the current session is in a connected state.
8. Criteria, Detchedcriteria, and query interfaces
Both the criteria and the instances of query are bound to the session, and their lifecycle ends with the end of the session.
The Detchedcriteria instance is equivalent to an SQL template for reuse. The Getexecutablecriteria (session) method receives a session object and binds to it, returning a criteria object.
9, Hibernate class of Initialize () method
The Initialize () method forces hibernate to immediately load the objects and collections associated with the specified entity. There are several other useful but not very common methods in the Hibernate class.
10. Lazy properties in the mapping file
In Hibernate3, the lazy property of the class element defaults to True, and if it is not needed, the display is specified as lazy= "false", otherwise the object returned by operation Load throws an exception. In addition, you can also specify lazy properties for entity properties in Hibernate3.
11. JDBC and JTA Services
Hibernate itself does not have transaction management capabilities, it relies on JDBC or JTA transaction management capabilities, in Hibernate configuration files, if you do not explicitly specify transaction factory class attributes Hibernate.transaction.factory _class configuration, the default is JDBC transaction:
<property name= "Hibernate.transaction.factory_class" >org.hibernate.transaction.JDBCTransactionFactory< /property>.
After the session is fetched through Sessionfactory, the JDBC Connection instance associated with the session is set to False.
Special NOTE: If the database does not support transactions, such as the MySQL MyISAM engine table does not support transactions, declaring transactions will not work. To enable MySQL5 tables to support transactions, you can specify that the engine type of the table is InnoDB. If it is study or research, it is best to use PostgreSQL 8.3 or DB2, Oracle.
A JDBC transaction is always associated with a database connection (or a session).
JTA transactions can span multiple data connections (or multiple sessions), which can be connected to different databases, and JTA transactions are generally managed by the container. Programming simply defines the bounds of the JTA transaction at the beginning and end of more than one unit of action.
Special NOTE: If you use a JTA transaction, you cannot use a JDBC transaction to manage the operation of each session, otherwise there will be an error. For the versatility of the program, it is generally used to build applications using JTA transactions, which use any environment. Of course, you can also use transaction agents to add transaction control to each JDBC action method. This also provides a great convenience for porting the program to JTA container transactions later. In fact, you can now use spring's transaction management, which is perfectly combined with hibernate.
The Ps:persist () method has no return value and the Save () method returns an object identifier.
The persist () method can only hold objects that are transient and persistent, and the Save () method saves any state objects.
In common: After the method is called, the state of the object becomes persistent.
The Get () method returns an instance of the object, and for the load () method, if an object of that identifier is present in the persisted context, returns an instance of the object, or the proxy for the objects, which contains only the object identifier.
Be careful with GET or load
Load can improve caching efficiency but is more likely to be problematic in the back because it is an agent
Get is a direct access db so the cache function is gone, but it's getting results right away. Less hassle, but frequent calls are bound to increase the pressure on the server