Java Reference
In-Depth Information
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
As we can see, NetBeans generates methods to create, read, update, and delete
JPA entities.
The method to create a new entity is called create() , and it takes an instance of
our JPA entity as its sole argument. This method simply invokes the persist()
method on EntityManager , which takes care of persisting the data on the JPA
entity to the database.
For read operation, several methods are generated. The findCustomer() method
takes the primary key of the JPA entity we wish to retrieve as its sole parameter,
invokes the find() method on EntityManager to retrieve the data from the
database, and returns an instance of our JPA entity. Several overloaded versions of
the findCustomerEntities() method are generated, and these methods allow us to
retrieve more than one JPA entity from the database. The version of this method that
does all the real work is the one that contains the following signature:
private List<Customer> findCustomerEntities(boolean all, int
maxResults,
int firstResult)
The first parameter is a Boolean, which we can use to indicate if we want to retrieve
all values in the database; the second parameter allows us to specify the maximum
number of results we wish to retrieve; the last parameter allows us to indicate
the first result we wish to retrieve. This method uses the Criteria API that was
introduced in JPA 2.0 to build a query programmatically. If the value of the all
parameter is false, then this method sets the maximum number of results and the
first result by passing the appropriate parameters to the setMaxResults() and
setFirstResult() methods in the Query object.
 
Search WWH ::




Custom Search