Hibernate (2) -- POJO object operations

Source: Internet
Author: User

POJO objects are actually our entities. This blog summarizes the framework's primary key generation policies for POJO objects and some simple addition, deletion, modification, and query operations on POJO objects.

I. There are three primary key generation policies in the Hibernate framework:


1. The database is responsible for generating the primary key (proxy primary key)


A, native: indicates which database is used to generate the primary key based on the set dialect. For example, MySQL uses the auto-increment mode, and the primary key fields must be of the integer type. In the Oracle database, will adopt the sequence growth mode.


B, sequence: indicates that the primary key is generated using the sequence of the database, which is applicable to Oracle and DB2 databases.


C. identity: indicates that the auto-increment primary key generation method is used. It is applicable to MySQL and SQL Server.

2. The Hibernate framework is responsible for generating the primary key value (proxy primary key ):


A, increment: indicates that the Framework provides counters, accumulates data, and obtains the primary key.


B. uuid: A 32-bit hexadecimal numeric string is generated by the framework based on parameters (such as IP address, JVM Virtual Machine startup time, system time, and counter.

3. the user provides the primary key value (natural primary key ):


Assigned: the business (itself) provides the primary key.

Of course, native, uuid, and Assigned values are commonly used here. When POJO class and table ing are set, the primary key is set, and the label is , In Label, for example:

 
 
  
  
 



2. Three states of POJO objects in the Hibernate framework:


1. Transient (temporary): Transient Object

The object is not associated with the database data and is not associated with the Framework. For example, we just created new User ();


2. Persistence: Persistent Object

Objects and database data are associated with the Framework. For example, we just saved session. sava (user );


3. Detached Object

Objects are associated with database data, but not with the Framework. For example, the user whose session is closed after being saved.

Let's take a look at the conversion of three POJO objects:


Iii. Hibernate: add, delete, modify, and query POJO in three states:

VcmRlcj0 = "1" cellpadding = "0" cellspacing = "0" valign = "top">

Instantaneous status

Persistence status

Free Status

Add (Save)

Yes

No (unnecessary)

No (unnecessary)

Update)

No

Automatically updated after modification. Manual update is not required.

Yes

Delete)

No

Yes

Yes

Query

No

Yes

Yes

1. Save ):

You can use session. save (user); To save the data, but here we want to mention that the time when the framework sends SQL statements is different for different primary key generation policies:

A, native: The insert statement is sent when the save method is called.

B. uuid primary key generation policy and assigned primary key generation policy: an insert statement is sent when a transaction is committed.

This is because the primary key generation time is different. Because native is generated in the database, it is sent earlier.

2. Update ):

Here we want to talk about the update of the Free object:

Uuid

Assigned

Native

Existing records

Send upate statement

Will query and judge, and then update

Send update statement

The record does not exist.

Sending statement, section exception

Query and judgment, and insert

Sending statement, section exception

3. delete ):

Here, you only need to provide a primary key. You can delete it based on the primary key id, as long as the id exists.


4. Query:

A. Primary Key query:

Get: The returned result may be a persistent object or null. Therefore, you need to make a non-null judgment on the result. It uses the cache to query immediately.

// If the get method is queried successfully, the returned object state is persistent.

Objectobj = session. get (User. class, "admin ");

Load: The returned result may be a persistent object or cglib proxy object or an exception. The cache is used and the default value is delayed loading.

// The first query result of the load method is stored in the cache, which supports delayed loading and higher efficiency, but mainly

Objectobj = session. load (User. class, "admin ");

B. Common query (object-oriented query): this document will be introduced later.

1. Query: HQL (HibernateQuery Language). HQL is an encapsulation of the SQL Language and an Object-Oriented Query Language. For example: SQL: select * from t_user (Table Name) whereusername (field) = "tom"

HQL: from User (Class Name) where username (attribute) = "tom"


// Query String hql = "from User u" by page; // HQL !!!!! Query query = session. createQuery (hql); int pageno = 3; int pagesize = 2; int index = (pageno-1) * pagesize; query. setFirstResult (index); // The first subscript query of a page. setMaxResults (pagesize); // page size // conditional query String hql = "from User u where u. usercode =? And u. userpswd =? "; // Object-Oriented Query HQL !!!!! Query query = session. createQuery (hql); query. setString (0, "admin"); // The index starts from 0. setString (1, "admin ");



2. Criteria: all operations are completed in an object-oriented manner.


// Query by PAGE
Criteria cra = session. createCriteria (User. class); cra. setFirstResult (0); // start cra indexing. setMaxResults (2); // Number of pages per page // sort Criteria cra = session. createCriteria (User. class); cra. addOrder (Order. desc ("username"); // condition query Criteria cra = session. createCriteria (User. class); cra. add (Restrictions. eq ("username", "aaa"); cra. add (Restrictions. eq ("usercode", "aaa "));


In summary, Hibernate performs simple operations on POJO objects, mainly by ing and simple configuration, and then using the methods in Hibernate for configuration. Here is a few simple examples that involve less knowledge. We can refer to the Hibernate help document for reference, which will help us a lot!

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.