Fourth. Getting Started with hibernate

Source: Internet
Author: User

Fourth. Getting Started with hibernate
4.1 Framework Technology
The concept of the 4.1.1 framework
A framework is a semi-finished product that provides a reusable public structure.
4.1.2 Mainstream framework
4.1.2.1 Struts Frame
4.1.2.2 Struts2 Frame
4.1.2.3 Hibernate frame
An excellent persistence framework that simplifies the task of saving object data to a database, or reading data from a database and encapsulating it into objects.
4.1.2.4 Spring Framework
4.2 Hibernate Introduction and environment construction
4.2.1 The concept of data persistence
Data persistence is a general term for converting an in-memory data model into a storage model and transforming the storage model into an in-memory data model.
4.2.2 Hibernate Framework and ORM
4.2.2.1 Hibernate Framework (Data persistence tool, Object Relational mapping framework)
4.2.2.2 O<->r<->m (object/relational mapping, data persistence technology)
Advantages and disadvantages of the 4.2.3 Hibernate framework Use Cases
4.2.3.1 Advantages
1) Hibernate function is powerful
2) Hibernate supports many object-oriented features
3) Good portability
4) Hibernate open source free
For medium to large projects
4.2.3.2 Disadvantages
1) Not suitable for use in data-centric applications for large-volume storage processes
2) Large volume inserts, modifications and deletions are not available for hibernate
Not suitable for small projects, nor for relational model design unreasonable, non-standard system
4.2.4 Hibernate Environment Construction
1. Download the required JAR package
2. Deploy the JAR Package
3. Create Hibernate configuration file Hiernate.cfg.xml
Common parameters:
Connection.url: Database URL
Connection.username: Database user name
Connection.password: Database Password
Connection.driver_class: Database Driver
Dialect: Configure the type of database used by hibernate
Current_session_context_class: Specify Currentsession () the session by whom to track management
Show_sql: The parameter is true when the program runs in the console output SQL statement
Format_sql: The parameter is true when the program runs when the console output is formatted after the SQL statement
4. Creating persistent classes and mapping files
4.3 using hibernate to complete the persistence operation
4.3.1 using hibernate to persist 7 big steps
1. Read and parse the configuration file
Configuration conf = new configuration (). Configure ();
Build a Configuration object based on configuration information for the default location
2. Read and parse mapping information, create Sessionfactory object
Sessionfactory SF = Conf.buildsessionfactory ();
Sessionfactory is responsible for creating session objects
3. Open session
Session session = Sf.opensession ();
Session is the basis for hibernate persistence operations
4. Start a transaction (additional deletions must be done, query optional)
Transaction tx = Session.begintransaction ();
5. Database operation
such as: Session.save ();//save operation
6. End Transaction
Tx.commit ();//Commit a transaction
7. Close session
Session.close ();
4.3.2 using Hibernate to query by primary key
4.3.2.1 Object Get (Class clazz,serializable ID);
Example://Load Data operation
Dept dept= (Dept) session.get (dept.class,new Byte ("11"));
4.3.2.2 Object Load (Class theclass,serializable ID);
Example://Load Data operation
Dept dept= (Dept) session.load (dept.class,new Byte ("11"));
Difference: If the load data does not exist, the Get () method returns a null, but the load () method throws an exception
4.3.3 using hibernate to realize the deletion and modification of database
4.3.3.1 using hibernate to achieve increased
Example://persistent operation
Session.save (dept);
Commit a transaction
Tx.commit ();
4.3.3.2 using hibernate for modifying, deleting
Example 1://gets the object to modify
Dept dept= (Dept) session.load (dept.class,new Byte ("11"));
Modify Information
Dept.setdeptname ("Administrative department");
Commit a transaction
Tx.commit ();
Example 2://gets the object to delete
Dept dept= (Dept) session.load (dept.class,new Byte ("11"));
Delete Information
Session.delete (dept);
Commit a transaction
Tx.commit ();
Additions and deletions must be done in a transactional environment
Three states of Java objects in 4.4 hibernate
4.4.1
Transient state: After an object is created, the object is not immediately persisted and has no association with the database's data
Persistent State: Object and Session Management, when managed by session
Free State: An object in a persistent state, separated from the management of its associated session
Conversion of the 4.4.2 State
4.4.2.1: Transient state transitions to persistent state
Instantaneous state transitions to persistent state using the Session object's Save () and Saveorupdate () after saving the object
4.4.2.2: Persistent state transitions to instantaneous state
Persistent state transitions to transient state when object is saved using Delete () of Session object
4.4.2.3: Persistent state transitions to free State
Persistent State uses the session object's evict (), clear (), close () to save the object, and the persisted state transitions to a free State
4.4.2.4: Free State transitions to persistent state
Free State Use the Session object's update (), Saveorupdate () after saving the object, the Free State transitions to a persistent state
4.4.2.5: Free State transitions to instantaneous state
Free State when the object is saved using the delete () of the Session object, the Free state transitions to the instantaneous state
4.5 Dirty Check and refresh cache mechanism
4.5.1 Dirty Check (Hibernate checks the persistent state object in the session to determine if the object's data has changed, which is called a dirty check)
Dirty objects (objects that have changed before and after the state are called Dirty objects)
4.5.2 Refresh Cache mechanism
1) When the application calls transaction's commit (), the session's flush cache method is called flush ();
2) flush the cache when the program explicitly invokes the flush () method of session.
4.6 Updating Data methods
1) Update ()--change information, no OID times wrong
2) Saveorupdate ()--also includes the function of Save and update method, use Save () in instantaneous state, call Update in Free State ();
3) Merge ()--copies the properties of a free State object into a persisted object,
Performs an update or insert operation and returns a persisted object if the incoming instantaneous object is saved and returned a copy of it

Fourth. Getting Started with hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.