Analysis of the entity State and API of JPA

Source: Internet
Author: User

Entity objects have the following 4 states, which migrate by invoking the Entitymanager interface method:

New state: The newly created entity object, which does not already have a persisted primary key, is not associated with a persistent context. Controlled state: A persistent primary key is already in place and the persistence context is established; Free State: Has a persisted primary key, but has not yet established a relationship with the persistent context; The delete state: has a persistent primary key that has been linked to the persistence context but has been scheduled to be deleted from the database.

API for Entitymanager

Here are some of the main interface methods for Entitymanager:

void persist (Object entity)

By calling the Entitymanager persist () method, the new entity instance is converted to a controlled state. This means that when the transaction where the persist () method is committed, the data for the entity is saved to the database. If the entity has been persisted, then invoking the persist () operation will not happen. If the persist () operation is invoked on an entity that has been deleted, the deleted entity is converted to a controlled state. If the persist () operation is performed on the free entity, the illegalargumentexception is thrown.

The persist () operation is invoked on an entity, and the corresponding cascade persistence operation is performed on the entity associated with the entity;

void Remove (Object entity)

Deletes a controlled entity by calling the Remove () method. If an entity is declared as a cascading deletion (Cascade=remove or cascade=all), the associated entity is also deleted. Calling the Remove () operation on an entity that is newly created will be ignored. If the Remove () operation is invoked on the free entity, the illegalargumentexception is thrown and the related transaction is rolled back. If the Remove () operation is performed on an entity that has already been deleted, it is also ignored;

void Flush ()

Synchronizing the controlled Entity data into the database;

T-merge (T entity)

Persisting an entity of a free State into a database and converting it into a controlled entity;

T Find (Class entityclass, Object PrimaryKey)

Query entity objects with primary keys, Entityclass are classes of entities, PrimaryKey are primary key values, such as the following code query topic entities:

Topic t = em.find (topic.class,1);

Query createquery (String qlstring)

Create a query object, such as the following code, based on the JPA query statement:

Query q= em.createquery ("" Select T from Topic t

WHERE t.topictitle like:topictitle ")");

Query createnativequery (String sqlstring)

To create a query object using the SQL statement of the local database, query returns a list result set with each row of data corresponding to a vector, after executing it through the Getresultlist () method.

Query

JPA uses the Javax.persistence.Query interface to represent a query instance, which is constructed by Entitymanager by specifying a query statement. The interface has numerous interface methods for executing data queries:

Object Getsingleresult (): Executes a SELECT query statement and returns a result;

List getresultlist (): Executes a SELECT query statement and returns multiple results;

Query Setparameter (int position, Object value): Binds a parameter in a query statement by a parameter position number, and if the query statement uses a command argument, you can use Query Setparameter (String name, Object value) method to bind a named parameter;

Query setmaxresults (int maxresult): Sets the maximum number of results to return;

int executeupdate (): If the query statement is a new, deleted, or changed statement, this method performs the update operation.

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.