Hibernate Knowledge Point Understanding

Source: Internet
Author: User

1. Is there a problem with the business logic layer?
1. Switch Database trouble
2.sql write up trouble
3. Our programmers do not need to focus on the database, only want to care about the business itself

Benefits of 2.hibernate
1. Programmers only care about business logic, make the role clearer
2. More clearly layered
3. More general modification, suitable for database platform switch
4. Object, database relation becomes Java object relation, more convenient operation
5. Performance assurance, hiernate may follow different database operations, but will be optimal for SQL statements
6. Increase the robustness of the program.

Three ways to open 3.hibernate
1. Have domain object >> mapping >> db (official recommendation)
2. Start with DB and use tools to generate mapping and domain object. (Most of the projects use this, first design database, under construction Class)
3. Start with the mapping file

4.POJO (Plain old/ordinary Java object Simple Java objects)
1. Using the Pojo name is primarily for EJB confusion
2 A primary key to uniquely identify
3. There are other properties
4. There is a corresponding Get/set method
5. Properties are generally decorated with private
6. There must be an argument-free constructor

5. For hibernate, the transaction must be used when updating, deleting, querying data, adding

6.session.load (Employee.class,id)
1. Reflection mechanism

7.hibernate requires that classes in domain must be instantiated
1. If not instantiated, the operation will not know that the operation is the record, the serialization can uniquely identify the object
2. Can be transmitted on the Internet and file transfer

Important classes and interfaces in 8.hibernate
Class 1.Configuration: Configuration conf = new configuration (). Configure ();
2.SessionFactory interface: sessionfactory SF = Cfg.buildsessionfaction (); now
Serviceregistry serviceregistry = new Serviceregistrybuilder ()
. Applysettings (Configuration.getproperties ())
. Buildserviceregistry ();
Sessionfactory = Configuration.buildsessionfactory (serviceregistry);
3.Session Interface:
The 1.Session interface is the most widely used interface for hibernate applications. The session is also known as the Persistence Manager,
It provides actions related to persistence, such as adding, updating, deleting, loading, and querying objects.
2. Not thread-safe, so you should avoid multiple threads sharing a session instance when designing the software architecture.
3.Session is lightweight, so-called lightweight, which means that it does not need to consume too much resources to create and destroy.
The session object is built with Sessionfactory:
4.Session Session = Sessionfactory.opensession ();

9. Understanding of Persistence classes:
There is no difference between a persisted class and a normal JavaBean class, but it is associated with the session and has the ability to persist.
This persistent operation is controlled by the session by loading, saving, creating, or querying the persisted object PO through the Session object.

The main methods of the 10.Session class are:
1. Create a Query object
CreateQuery () method: Used to generate a query object from the session.
BeginTransaction () method: Used to generate the transaction object from the session.
2. How to manage the session:
IsOpen (), flush (), clear (), evict (), and Close () methods.
3. Get persisted objects: get () and load () methods.
Get () method
public object Get (Class entityclass,serializable ID)
Entityclass indicates the type of the class, the ID is the primary key value of the object, and if the ID is of type int, it should be converted to an integer object by means of the new Integer (ID).
4.load () method
public object Load (Class entityclass,serializable ID)
Both the load () method and The Get () method can load a persisted object PO from the database with the primary key ID value.
The main difference between the two: when the primary key ID value does not exist in the database, the load () method throws the Org.hibernate.objectnotfoundexceptio,get () method and throws the Java.lang.NullPointerException.
5. Save, update, and delete persisted objects:
Save (), update (), Saveorupdate (), and Delete () methods.

11. Understanding the Sesion Cache
A series of Java collections are defined in the implementation class ORG.HIBERNATE.IMPL.SESSIONIMPL of the session interface, which forms the cache of the session.

The three main functions of 12.Session cache
1. Reduce the frequency of access to the database.
2. Ensure that the objects in the cache are kept in sync with the related records in the database.
3. When a cyclic relationship exists in a persisted object in the cache, the session guarantees that there is no dead loop of access to the object, and that the JVM stack overflow exception is caused by a dead loop.

13. Note: The difference between the commit () and flush () methods of the session:
1..flush (): Cleans up the cache and executes a series of SQL statements without committing the transaction.
2.commit (): The Flush () method is called before the transaction is committed. Committing a transaction means that the updates made by the data are permanently saved.

14.ORM: Object Relational Mapping

15.openSession () and getcurrentsession () differences
The session created by ① with Getcurrentsession () is bound to the current thread, while the session created with Opensession () will not
The session created by ② with Getcurrentsession () is automatically closed at commit or rollback, and the session created with Opensession () must be closed manually.
③ using getcurrentsession () needs to include in the Hibernate.cfg.xml file
The following configuration:
* If you are using a local transaction (JDBC Transaction)
<property name= "Hibernate.current_session_context_class" >thread</property>
* If you are using a global transaction (JTA Transaction)
<property name= "Hibernate.current_session_context_class" >jta</property>
Simply explain the differences between JDBC and JTA transactions.

Hibernate Knowledge Point Understanding

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.