Hibernate (5) core development interface and hibernate Interface

Source: Internet
Author: User

Hibernate (5) core development interface and hibernate Interface

I. Configuration

1. AnnotationConfiguration

2. Manage configuration information

3. Use the configure () method by passing in a configuration file, for example, new Configutayion. configure ("myhibernateconfig. xml ")... if the configuration file is not input, the default value is hibernate. cfg. xml

 

Ii. SessionFactory

1. Maintain the database connection pool. When a session is generated, a connection is generated from the database connection pool and handed over to the session.

2. getCurrentSession (): If a session already exists in the current context, you can directly obtain this sesssion. If no session exists, a session is created. (The session is executed before the session is submitted. getTransaction (). the same session is obtained before commit (). When a session is submitted, the session is closed and the new session is obtained ). Automatic transaction commit close ()

4. Context: Specify the hibernate. current_session_context_class attribute in the configuration file.

There are four values: jta | thread | managed | custom. Class

Jta (java transaction api): java API used to manage transactions (Distributed Database transaction Management)

If the value is thread, when getCurrentSession () is executed, the session will be retrieved from the current thread. If no session is found, the session will be created.

3. openSession (): a new session will always be created. Close () is required ().

(Note: first, openSession () gets a session1, and then getCurrentSession () gets a session2. The two sessions are not equal to or equal. Because session1 indicates that the session needs to be closed, and session2 indicates that the session cannot be closed, the implementation classes of the two sessions are different)

 

3. Three statuses of Objects

Transient (temporary status): It is just new. An object in the memory, no id or cache (session cache)

Persistent (Persistence State): After other methods such as sava () are executed, they are converted to the Persistent state, which has an id. Memory, cache, database, and id.

Detached (out of management state): After other methods such as close (), the status changes to this State. Memory, cache (out of management), database, and id

Example:

Teacher t = new Teacher (); // t: Transient status t. setName ("test1"); // t: Transient status t. setTitle ("title1"); // t: Transient status Session = sf. openSession (); session. beginTransaction (); session. save (t );
System. out. println (t. getId (); // t: session. getTransaction (). commit ();
System. out. println (t. getId (); // t: Detached status session. close ();

 

2. Differences between the three States

(1) Is there any id;

If no id is available, it must be in the Transient status.

(2) is the id in the database;

(3) is the id in the memory (session cache );

 

Iv. Session

1. Manage a database task unit (session helps us manage and curd)

2. Method (CURD)

(1) delete

The execution of the delete () method will change to the Transisent state (refer to the three-state diagram), and can be deleted as long as there is an idnumber.

(2) load (Class arg0, Serialirable arg1). The first parameter is the Class, and the second parameter is the primary key.

Example: Teacher t = (Teacher) session. load (Teacher. class, 1 );

Note:

Teacher t = (Teacher)session.load(Teacher.class , 1);session.getTransaction().commint();System.out.println(t.getName());

This write method throws the LazyInitializationException: cocould not initialize proxy-no Session exception.

The reason is that load returns the proxy object. When the load method is executed, it does not issue an SQL statement, but waits until the t object's attributes are retrieved (for example, getName). In the example, when getName () is executed, the session transaction has been committed.

  

(3) get (Class arg0, Serialirable arg1). The first parameter is the Class, and the second parameter is

Example: Teacher t = (Teacher) session. load (Teacher. class, 1 );

When the get method is executed, an SQL statement is immediately issued, and then the value is taken from the database and loaded into the object t.

Related Article

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.