Getting Started with Hibernate (ii) three statuses

Source: Internet
Author: User
Tags uuid

. Large configuration
Dialect
Property name= "dialect" value
automatically build table structure
Property name= "Hbm2ddl" true
Connection_url: Connection string
Show SQL
show_sql
Format SQL
Format_sql
2.Hibernate test class steps
7 steps
1. Read the configuration file, form the Config Object
2. Sessionfactory
3. Get session
4. Open a transaction
5. Execute Action
6. End (commit or rollback)
7. Close
3. Small configuration
<class name= "Dept" table= "Dept" >
<id property= "Deptno" column= "Deptno";
Primary key generation policy: Database
<generator class= "native" ></generator>
</id>
</class>

4.idea introduce dependency (used Oralce)
1. Manual jar package registered in the Warehouse
1. There is a MAVEN environment
2. Modify the path in the Conf/settings.xml to the path of your own repository
3. Register the name MVC insatll:install-file-dgroupid
2, direct physical reference
5. Query by primary key
Get
Load

Difference 1:load: proxy object
Get: The original object itself is always returned

Difference 2: If the Load/get loaded record data does not exist, get returns NULL. Load throws Exception Objectnotfoundexception

6. Add
Considerations: Requires a transactional environment
Session.save (dog);

7. Two options for getting a session are added
Opensession ()
Getcurrentsession ()---------------> You have to be in a large configuration.
<property name= "Current_session_context_class" >thread</property>
result, you do not have to manually close the connection.
--------------------------------------------------------------
Modify
Update ()
Merge (): Save () update () No OID
Saveorupdate () Modify

Saveorupdate () and merge () differences
Parsing: The difference between the 1:saveorupdate () changes the state of the incoming object and the merge does not
Difference 2:saveorupdate () No return value merge returns an object

2.OID
An OID is a persisted class (Student) corresponding to a data table primary key property that uniquely distinguishes a persisted object.
3. Delete

4.PO VO DTO POJO JavaBean
Javabean=pojo and Database-independent pageutil classes that need to be persisted
Po=pojo+xml

5. Primary key generation Policy
Increment:hibernate generation, Disadvantage: multithreading may take the same ID, but cause other threads to save failed
Identity: Database generation, MySQL support, Oracle not supported
Sequence: Database generation, Oracle support
Native: Database decision: MySQL uses identity,oralce to use sequences
UUID Hibernate Build Pros: No duplicates, better data migration requirements: column type string
Assigned: The programmer is manually operated.

Oid
Three states of Java objects in hibernate
Get and load



/**
* Operate on increment basis!
* Three statuses in hibernate
*
* 01. Instantaneous State Student stu=new Student ();
* The newly created object is not associated with the session!
* 02. Persistent Status Session.save (Stu)
* Managed by session, exists in session cache! Commit to the database when committed!
* 03. Free State Session.close (); Stu
* Once managed by the session, but now there is no session cache!
*
*
* The difference between transient state and Free State is: whether the session has been managed! Whether the object has an OID (object identifier)!
*
* The object has a primary key value ID, then there is an OID, the operation will be executed when the UPDATE statement!
* Without the primary key value ID, then there is no OID, the operation will be executed when the INSERT statement!
*
* Object As long as there is no OID, in the implementation of update () definitely error! The Save () method is executed when the saveorupdate is executed!
*
*/

6. Core API
6.1 Configuration
InputStream x = Confighelper.getresourceasstream ("/hibernate.properties");
Hibernate.hbm.xml configuration file

6.2 Sessionfactory: Heavyweight, one copy of the program. Thread-safe.
Creates Sessions. Usually an application have a single sessionfactory.
Threads Servicing client requests obtain Sessions from the factory.

Create a session. Typically, the application has a single sessionfactory.
The service client requests the thread to fetch the session from the factory.
Sessionfactorys is immutable. The behaviour of a sessionfactory
is controlled by properties supplied at configuration time.
These properties is defined on environment.

The Sessionfactorys is immutable. The behavior of Sessionfactory is controlled by the properties provided at configuration time.
These properties are defined in the environment.

Thread-Safe:: Method to use an object without regard to thread safety issues
Member variable, found that he is an interface, the interface is a method

6.3 Session He is also an interface, the implementation class is Sessionimpl, which is also the method, but he is thread non-security.
Session Building two Ways

Mode one: Factory.opensession (); Randomly gets a connection from the connection pool.

Factory.getcurrentsession (); Must get the session bound to the current thread.

6.4 Business

7. Refresh the cache (action triggered at a point in time)
12: Go to bed, take off your clothes
28 married into the bridal chamber brain Hole Open, oneself think. Otherwise it's not fun (how to enter the bridal chamber)
Tx.commit (); Refresh cache refresh Cache What to do (dirty check)

A snapshot (Snapshot) is an image of a dataset at a particular point in time, also known as an instant copy, which is a fully usable copy of the dataset

1. Modifications
Update
Merge ()
Saveorupdate ()

2. Delete

3.PO DTO VO POJO JavaBean

4. Primary key generation Policy
Native
Uuid
Assigned

5. Three states of Java objects in hibernate
Instantaneous state
Persistent state
Free State

Transient state----> Persistence Save () load () get ()

Persistent state----> Free State Clear () Close () evict ()

Free State----> transient state Delete ()

Instantaneous state and Free State? There is no OID
A unique flag for a 6.OID persisted object. The OID must be bound to the primary key value recorded in the underlying data table.

7.Hibernate API
Configuration class

Sessionfactory Interface Sessionfactoryimpl

Session Interface Sessionimpl
Session:getcurrentsession () The current thread binding dispatches multiple memory addresses, the same object
Opensession (): Each time you get to the session must be new.
Transaction

Getting Started with Hibernate (ii) three statuses

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.