Hibernate the next day

Source: Internet
Author: User

1. Modifications
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 a increment basis!
Three states in Hibernate
Transient state: There is no associated data in the database and is not added to the session.
Persistent state: Managed by session, with database identity.
Free State: Out of the management of the session associated with it.

01. Instantaneous State Student stu=new Student ();
The newly created object is not associated with the session!
02. Persistent State Session.save (Stu)
By session management, there is the 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: Is it managed by the session? Whether the object has an OID (object identifier)!

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, there is no OID and an INSERT statement is executed at the time of operation!

Object as long as there is no OID, in the execution update () definitely error! The Save () method is executed when the saveorupdate is executed!

Conversion of three states:
(1). Instantaneous state transitions to persistent state
When the object is saved by using the session's Save (), Saveorupdate (), the state of the object has a transient state to a persistent state.
Gets the object using the Get () or load () method of the session, the State of the object is persistent state
(2). Persistent state to instantaneous state
After the Delete () method of the session is executed, the object is changed from the original persistent state to the instantaneous state, so the object is not associated with any database data.
(3). Persistent state to Free State
Executes the evict (), clear (), or close () method of the session (), and the object is moved from the previous persistent state to the Free State.
(4). Free State to permanent turn
Executes the update () or Saveorupdate () method of the session, where the object is moved from a free State to a persistent state, and the object is associated with the current session again.
(5). Free State to instantaneous state
The delete () method of the session is executed, and the object is shifted from the Free State to the instantaneous state.

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. The session must be new every time you get it.
Factory.getcurrentsession (); Must get the session bound to the current thread. Dispatched multiple times, same as memory address, same object

6.4 Business

7. Refresh the cache (action triggered at a point in time)
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


Configuration CFG;
Session session;
Transaction TX;
Sessionfactory Factory;
@Before
public void Mybefore () {
Creating a Configuration Object
Cfg=new Configuration (). Configure ();
2. Create sessionfactory based on the configuration object
Factory=cfg.buildsessionfactory ();
3. Create session according to Sessionfactory
Session= factory.opensession ();

3.5 Turn on Transaction xxx0001 after session creation xxxxx003
tx= session.begintransaction ();
}
@Test
public void LoadTest () {
Dog dog = Session.get (Dog.class, 6);
System.out.println (Dog.getdogname ());
}
@Test
01. Modifications
public void Testupdate () {
Modify a dog's information for a number 2
Transaction tx = Session.begintransaction ();
Dog Dog=new Dog ();//transient State
Dog.setdogname ("hehe");
Dog.setdogage (12);
Session.merge (dog);//Persistent state
/* Dog dog=session.load (dog.class,2);
Dog.setdogname ("Rhubarb 3");
Dog.setdogage (33);
Session.update (dog); * *
Tx.commit ();
System.out.println ("Update ok!");
}
02. Delete
@Test
public void Testdelete () {
Modify a dog's information for a number 2
Transaction tx = Session.begintransaction ();
Dog Dog=new Dog ();//transient State
System.out.println (session+ "===========================");
/* Session.delete (dog);
Tx.commit (); */
System.out.println ("del ok!");
}
@Test
public void Testcahce () {
Dog dog2 = Session.get (Dog.class, 6);
System.out.println (Dog2.getdogname ());

Dog Dd=new Dog ();
Dd.setdogage (11);
Dd.setdogname ("Floret is a Dog");
Dd.setdogid (11);
Session.update (DD);
Tx.commit ();
Dog dog = Session.get (Dog.class, 6);
System.out.println (Dog.getdogname ());
System.out.println (Dd.getdogname ());
//
}

Hibernate the next day

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.