Java persistence API (JPA)
Version 2.0 is currently in the draft final proposal stage, which makes important updates to the specification for the condition query (Criteria
Queries) adds an API, A Metadata API, and supports bean validation [JSR 303].
This version of conditional query (Criteria
Query) the API allows you to create a query by calling the Java object method, instead of embedding the JPA-QL into the string, which is processed by the JPA implementation. In the first public draft, the API
The Group itself is string-based and has received a lot of feedback asking them to study possible ways to improve the type security of the motion. Gavin
King puts forward a suggestion based on the existing string-based API, using Java
6
Javax. annotation. processor in automatically generates a metadatabase type for each persistent class in the application. Combined with generics, this mechanism provides an effective
The work und solves the problem of Java's lack of class-oriented methods and Attribute-type security meta-model. Every time a code generator is used, it will cause controversy, because the code generated by it is often obscure and usually
It must be supported by a specific IDE. However, it is possible to keep the generated code clean because it is only used for type information. For example, given a persistent class:
import java.util.Set;
import java.math.BigDecimal;
@Entity public class Order {
@Id Integer orderId;
@ManyToOne Customer customer;
@OneToMany Set lineitems;
Address shippingAddress;
BigDecimal totalCost;
...
}
The corresponding typical metamodel class, order _, is as follows:
import java.math.BigDecimal;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Set;
import javax.persistence.metamodel.TypeSafeMetamodel;
import javax.annotation.Generated;
@Generated
@TypeSafeMetamodel
public class Order_ {
public static volatile Attribute
Because Java 6 contains the annotation processor, this change does not require specific ide support-the IDE can delegate the annotation to the annotation processor triggered by the compiler, and generate a metadata model in the processing process.
The meta-model can be queried through an API, which specifies an interface for dynamic access to the persistent State meta-model and the persistent unit hosting class (managed
Classes. It cannot provide support for accessing ORM information, even though it is in the future update plan. This interface is implemented by the persistence provider.
Entitymanagerfactory. getmetamodel and entitymanager. getmetamodel
The supplier implements the javax. Persistence. metamodel. metamodel interface.
King recently suggested
Further refactor the row, extend the type security to the query result set, and add a type parameter to both criteriaquery and query. This allows developers to directly access type-safe instances.
Body list or encapsulation, but for projects based on JPA 1.0, this will cause the compiler to warn, because javax. Persistence. query currently does not have this type parameter.
Type security methods have aroused debate in the Expert Group. Linda demichiel, the leader of JPA 2.0 standards, described the comparison between string and type security methods in her blog. In this article, she described:
"After several controversies over meta-model-based APIs and string-Based APIS, we [Expert Group] decided to adopt Type-safe APIs. However, if developers prefer, we also provide options based on the string method."
The main difference between the two options is that string-based Attribute names can be used to specify attributes referenced in joins and navigation. The result is that these queries have the same semantics as the type security edition, but they do not have the same level of type security.
JPA 2.0 also supports bean validation [JSR
303]. Managed classes (entities, ing superclass, and embedded classes) can be configured to include beans
Validation constraints. The automatic validation with these constraints is implemented in this way, specifying that Java persistence applications delegate the validation to pre-persist, pre-
Bean validation Implementation of update and pre-Remove object lifecycle events.
The Group is eager to receive feedback from the community, which can be sent to the jsr-317-pfd-feedback@sun.com if any.
From:. Net World