Basic concepts of Hibernate

Source: Internet
Author: User

Basic concepts of Hibernate

1. Three types of files required for data development[Theoretically, either of them can be generated using tools]:

(1) domain-class user

(2) ing file user. HBM. xml

(3) Create Table user (....);

 

2. Order of these three types of files (three development processes ):

(1) By domain object à mapping à dB (officially recommended) [starting from object-oriented]

(2) Start from the DB and use tools to generate mapping and domain object (more) [from the perspective of relational model]

(3) Start from the ing file ---- generation table ---- generation Domain Layer

 

3. Restrictions on object class objects:

(1) default constructor (required) [either do not write the constructor or write the constructor without parameters. Use newinstance () in the reflection mechanism to create a specified type of object class clazz = user. Class; user = clazz. newinstance ();]

(2) meaningless identifier ID (primary key) (optional)

(3) Non-final, which has an impact on the lazy loading (optional) (a subclass is automatically generated during the lazy loading and continues. When final is used, it cannot be inherited, all lazy loads will be ineffective)

 

4. ing file:

A Java class is represented by a pair of <class> labels in the ing file. If there are multiple Java classes, there should be multiple <class> labels in the ing file.

These <class> labels can be placed in a ing file or multiple ing files.

Ing file name -------- class name. HBM. xml [this name is easy to read]

 

5. Coding

Transactions are generally not required for queries.

Tool class hibernateutil

Static{

Configuration CFG =NewConfiguration ();

Cfg. Configure ();

Sessionfactory= Cfg. buildsessionfactory ();

}

 

6. Common methods in the session Interface

Begintransaction (); // starts the transaction and returns the transaction object.

Clear (); clear Cache

Contains (); determines whether the buffer contains the specified object

(1) Save and persist: when a transaction is started, both save and persist are saved. When a transaction is not started, save () the method generates an insert statement and performs the insert operation. However, since no transaction is committed, all the statements are eventually rolled back, and persist () does not generate an insert statement outside the transaction.

(2) Delete: delete an object

(3) Update: update an object. If no record exists in the database, an exception occurs.

(4) Get, query by ID. When a session is queried, the session can only be queried by ID. To perform complex queries like this, you cannot use the session object. You need to use hql or criteria

(5) load, query by ID (proxy is returned and the database is not accessed immediately)

Difference between get () and load (): The load method will not connect to the database immediately, but will connect to the database for the first time, and the get method will connect to the database immediately

(6) saveorupdate, merge (based on the value of ID and version to determine whether it is save or update), calling merge, your object is still out of control

(7) Lock (changes an object to a persistent object, but does not synchronize the object state)

 

7. Object status:

(1) Instantaneous State: the new object is an instantaneous state.

User user = user (); // user is an instantaneous state

No records in the database table correspond to the instantaneous object.

(2) persistent State: Session. Save (User );

The database has corresponding records, and the current object is associated with the session, which is managed by the session. As long as the user object has any changes, Hibernate knows, the database will also change.

(3) de-managed status: a record in the database table corresponds to an object, but it has no connection with the session and is not managed by the session. The user object changes and hibernate does not know it.

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.