is a named query with annotations in the Java Persistence API really useful?

Source: Internet
Author: User
Tags comments object model

Comments on annotations

The Java Persistence API (JPA) defines several ways to access data: Through an entity manager, through JPA-QL, or through a native query. In JPA, annotations are used as a mechanism for mapping Java objects to the underlying database. You can also provide XML metadata as an overlay or an alternative mechanism for mapping annotations. However, most of the JPA uses I see are obviously like using annotations. The fact that a specification document uses annotations rather than using an xml-based mapping example (which only shows you an XML Schema) to represent all the examples may be one of the reasons for overwriting. Create an Object relational mapping to abstract the details of the underlying database from the Java object model. However, JPA allows database details to be quickly returned to the Java source. In this article, you will examine the various query styles in JPA, explain why they exist, and explain why it makes no sense to comment on certain styles, such as named queries. The final conclusion is that this small example is actually part of a larger problem.

Accessing Data using JPA

Let's take a quick look at the various ways to access data using JPA, assuming you are familiar with how to map Java objects in JPA.

Entitymanager

The application interacts with the Java object at run time. By using a special object called an entity manager, an application can query or persist an object. The Entitymanager instance is associated with a persistent context. In a permanent context, the entity instance and its lifecycle are managed. Entitymanager can be considered a façade of the underlying permanent mechanism. Entitymanager contains the necessary methods to access data. The easiest way to access persistent data is to use the Find method. Here is an example of an application that uses the entity manager to find objects through a primary key:

Customer customer = (customer) em.find (Customer.class,customerid);

The Find method requires that you know the primary key and the type of the actual class.

JPA-QL Query

JPA also has a full-featured query language that can be used for object models. The JPA query language contains many features for more complex queries. You can pass a query dynamically to the entity manager:

Query q = em.createQuery("SELECT c FROM Customer c WHERE c.name LIKE
    :custName");
q.setParameter("custName", name);
q.setMaxResults(10);
List result = q.getResultList();

The ability to pass queries at run time is required for some dynamic scenarios, such as unknown conditions. However, in most cases, you want to lock the query based on the entire performance test.

Native query

JPA also enables you to use native SQL queries against the underlying tables and to provide the ability to map back results:

Query q = em.createNativeQuery(
"SELECT o.id, o.quantity, o.item, i.id, i.name, i.description "+
"FROM Order o, Item i " +
"WHERE (o.quantity > 25) AND (o.item = i.id)",
"OrderItemResults");

Standard SQL is required in many cases. I've given a lot of reasons in my previous Comments column.

Related Article

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.