A common API for self-learning JPA-04-JPA

Source: Internet
Author: User

JPA related interfaces/classes:

Persistence

    • The persistence class is used to get the entitymanagerfactory instance. The class contains a static method named Createentitymanagerfactory.
    • There are two overloaded versions of the Createentitymanagerfactory method.
      • A method with one parameter to the persistence unit name parameter in the JPA configuration file Persistence.xml
      • A method with two parameters: the previous parameter has the same meaning, and the latter parameter, the map type, is used to set the related properties of the JPA, ignoring the properties set elsewhere. The property name of the Map object must be the property name of the JPA implementation Library provider's namespace convention.

Entitymanagerfactory

    • The Entitymanagerfactory interface is primarily used to create Entitymanager instances. The interface contracts the following 4 methods:
      • Createentitymanager (): Used to create an instance of an entity manager object.
      • Createentitymanager (Map map): An overloaded method for creating an instance of an entity manager object, which is used to provide Entitymanager properties.
      • IsOpen (): Check if Entitymanagerfactory is open. The Entity Manager factory is open until it is created, unless the close () method is called to close it.
      • Close (): Entitymanagerfactory off. All resources will be freed after Entitymanagerfactory is closed, the IsOpen () method test will return false, other methods will not be called, otherwise the illegalstateexception exception will be caused.

Entitymanager

  • In the JPA specification, Entitymanager is the core object to complete the persistence operation. An entity is a normal Java object that becomes persisted only when the entitymanager is called to persist it. The Entitymanager object manages the O/R mapping between a set of entity classes and the underlying data source. It can be used to manage and update entity beans, find the entity bean by the primary key, and query entities through the JPQL statement.
  • Status of the entity:
      • New state: The newly created object, which does not already have a persistent primary key.
      • Persistent state: already has a persistent primary key and has established a context for persistence
      • Free State: Has a persistent primary key, but does not establish a context with persistence
      • Delete State: Has persisted primary key, has established and persisted the context environment, but deletes from the database.
    • Find (class<t> entityclass,object PrimaryKey): Returns the entity class object for the specified OID, returns a cached object if the entity exists in the current persisted environment, or creates a new Entity, and loads the relevant information in the database, or returns a null if the OID does not exist in the database. The first parameter is the entity class type being queried, and the second parameter is the primary key value of the entity to be found.
    • GetReference (class<t> entityclass,object PrimaryKey): Similar to the Find () method, the difference is that if the specified Entity does not exist in the cache, Entitymanager creates a Entity class, but does not immediately load the information in the database, only the first property that actually uses this entity is loaded, so if the OID does not exist in the database, getreference () does not return a null value. Instead of throwing entitynotfoundexception
    • Persist (Object entity): Used to incorporate newly created entity into Entitymanager management. After the method executes, the Entity object passed into the persist () method is converted to a persisted state.
      • If the Entity object that passed in the persist () method is already persisted, the persist () method does nothing.
      • If the persist () operation is performed on an Entity that deletes a state, it is converted to a persisted state.
      • If the persist () operation is performed on an entity that is free, the persist () method may throw a entityexistexception (or it may be thrown after flush or transaction commit).
    • Remove (Object entity): Deletes the instance. If the instance is managed, which is associated with a database entity record, the associated database record is also deleted.

A common API for self-learning JPA-04-JPA

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.