JPA Entitmanager Advanced

Source: Internet
Author: User

in the last chapter, we briefly introduced JPA Entitymanager the basic operation, Let's proceed today with a deeper level of introduction to Entitymanager.


1 Cascade Operations

2 Entity Status

3 Data Synchronization

1 cascade operations 1.1persistence of relationships and entities

Last time, we just persisted a customer entity, How do you do it if you create a customer and then persist the corresponding address entity for the customer? many entities in the JPA that have a persistence relationship

First look at the following customer-to-address relationship

Code

Publicclass Customer implemets serializable{//cascading persistence @onetoone (cascade = cascadetype.persist) Private  Address Address }

Cascade , when calling persist method to persist the client entity

Cascade = cascadetype.persist, the entity associated with the customer is also automatically


Persisting Customer Entities


  Customer.setaddress (address);  Em.persist (customer);

In this way, only the client entity needs to be persisted, and the corresponding fixed entity will be automatically persisted without having to persist two times

Entity properties and how to load

once the entity has passed Find after a method query, the properties of an entity can be loaded in two ways, are instant loading (EAGER) and lazy loading (Lazy),


fetch = FETCHTYPE.E LAZY Lazy Loading

Fetch = Fetchtype.eager Instant Load


for LAZY loaded, the query is executed only when the Entity property is used SQl , the property is loaded in, and if the call is not displayed, the property is not loaded into the customer entity, and when the entity has multiple data, using lazy loading, it is easy to cause frequent access to the database, which appears n+1 problem. If the amount of data is small, this way is still a good choice, if in a way not absolutely good or bad

1.2 Updating Entity Relationships

earlier we said that when persisting the entity content of a relationship, we learned that by setting the entity relationship cascading Properties Cascade can call persist method to automatically persist entity relationships, It is also used for updating entity lock correspondence. the Merge method updates the relationship entity corresponding to the entity at the same time

Publicclass Customer implemets serializable{//cascade Update Settings @onetoone (cascade = cascadetype.merge) Private  Address Address }

at this point the client can find the customer entity by first Get or Set method to modify the address entity, you can realize the correction of the customer address.

Cusetomer customer = Cuseomerbean.findbyid ("UUID"); Customer.geteAddress.setZip ("056000"); Cuseomerbean.updateentity (customer);

when calling Merge method, the entity manager not only checks whether the client entity modifies i Ah, but also examines the lock-associated address entity, which, if transient, automatically persists and then saves, if managed, to the data. For example, a new address entity is created and can be persisted.

  Cusetomer customer = Cuseomerbean.findbyid ("UUID"); Address address = new address (); Address.setzip ("056000"); Customer.setaddress (address); Cuseomerbean.updateentity (customer);

Method operation behind or to look at the phenomenon, or understand it does not know what it is


2 Entity Status

An entity has undergone multiple states from creation to destruction, entitymanager If we manage these entities, let's now uncover the veil.

usually there are several states transient state (transient), managed (managed ) and destruction ( removed) , with Hibernate vastly different

To illustrate:

Transient state: The object is not saved in the database, through the NEW object, in memory, for persistence

Persistent state: relative transient state, called persist , saved in the database

Managed State ( Managed): is managed by the context (persistence Contexgts), which is within the context of which the entity can be managed.

Free too: relative to the managed state, when the entity is not in context, is in a free State,

Free from too much condition:

    • end of transaction, entity out of scope
    • Copy entity or serialization, entity in Free State
    • Call clear, there are entities that are in a free State

Destroy state

An entity is destroyed after it is removed from the database, but must be deleted in the managed state, or an exception is thrown


3Data Synchronization

Perhaps you would think that when calling the persist,merge or mremove method, have already saved the entity to the database, but this is not the case, the knowledge of these methods changed the state of the entity, and ultimately saved to the database, using the flush Method

When to use this method, you need to know Flush How to submit , the default is

AUTO Auto-Femdom, The Entity manager calls the flush method, based on the end of the transaction .

Setting mode is

Publicenum flushmodetype{AUTO; COMMIT}


Summary:

Understanding the Entity Manager Cascade Operation and life cycle management, the period has a basic concept, while the set of some relationships have a sense of the overall, there is a promotion, the next Promotion Advanced

JPA Entitmanager Advanced

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.