Core development interface introduction and core development interface
Core development interface Introduction
1. hibernate_0500_CoreAPI
2, HiberanteAPI documentation needs to download the online api link: http://docs.jboss.org/hibernate/core/3.3/api
3. Configuration
A) AnnotationConfiguration
B) Manage configuration information
C) used to generate SessionFactory
D) You can specify the hibernate configuration file in the configure method.
E) you only need to pay attention to one method: buildSessionFactory ()
4. SchemaExport
5. SessionFactory
A) used to generate and manage sessions
B) Generally, only one SessionFactory is required for each application.
C) unless you want to access multiple databases
D) follow two methods: openSession getCurrentSession
I. openSession is new every time and needs to be closed
Ii. getCurrentSession is located from the context. It is useful for the old and has not been created.
1. Purpose: Define transaction boundaries
2. Automatic close of transaction commit
3. current_session_context_class (jta/java transaction api thread)
A) thread uses connection
6. Session
A) Manage the task units of a Database
B) method (CRUD)
I. save
Ii. delete
Iii. load
Iv. get
Differences between v. get and load
1. If no corresponding record exists, the performance is different.
2. The load returns a proxy object. The SQL statement is issued only when the object content is actually used.
3. get is loaded directly from the database without delay
Vi. update
1. Used to update the detached. After the update is complete, it is converted to the persistent status.
2. An error will be reported when updating the transient object (the database has corresponding records)
3. Update the transient object with the configured id.
4. Fields in the p state will be updated as long as different fields are set.
4. Update partially changed Fields
A) xml sets the update attribute of the property tag, while annotation sets the updatable attribute of @ Column. However, this method is rarely used and inflexible. Configuration: @ Column (updatable = false)
B) Use dynamic-update in xml. JPA 1.0 Annotation does not have the corresponding attribute. Configuration: <class name = "Student" table = "student" dynamic-update = "true">
Hibernate extension?
I. The same session can be used, but not across sessions, but merge () can be used (not important)
C) use the content of HQL (EJBQL) (recommended. Example: Query query = session. createQuery ("update Student s set s. name = 'z5' where s. id = 1 ");
Iv. saveOrUpdate
Vii. The find method is out of date!
Ix. clear Method
1. Whether it is load or get, the system first searches for the cache (level-1 cache). If not, the system searches for the database. The clear () method can be called to forcibly clear the session cache.
Ixi. flush Method
1. Force synchronization from memory (session cache) to database
2. flushMode
X. You can call the flush method to forcibly synchronize data from memory to database!
7. SchemaExport
Command line tool for exporting table mode to the database. This class can also be called from within the application.
Code: new SchemaExport (new AnnotationConfiguration (). configure (). create (false, true );
Create method:
View Code
8. Query interface
A) refer to the content of the Hibernate query (hql ejbql ).
8. Note:
A) There are many very detailed differences in Hibernate design, but they are rarely used in practical applications. Please enjoy the fun of writing projects and then discuss these details.
I. For example, the difference between save and persist
Ii. merge, evict, and other methods
Iii. Such as refresh and lock
B) recommended learning method: Hands-on Experiment
C) For details, refer to the supplementary video.
Three statuses
1. Previous project
2. The key to distinguishing the three states is
A) Are there any IDs?
B) is the ID in the database?
C) Is there a session cache in the memory)
A) transient: an object in memory, no ID, no cache
B) persistent: memory, cache, database, and ID
C) detached: memory, cache, database, and ID
3. What are the results of data operations in the changed state? For example, Will an update statement be issued if the attribute value is changed?
A) It is strongly recommended that you perform a hands-on experiment.
B) Think of normal people
C) Never back these things! It doesn't mean you have many cows!
4. Call the flush method to force synchronization from memory to database!
Jar package link: https://pan.baidu.com/s/1bprKHav password: 3fcx
Link: https://pan.baidu.com/s/1pKL8om3 password: k7wx