Java Reference
In-Depth Information
Retrieve the query result List of Edition entity instances using the getQueryList
method of the Query object and iterate over the result list using the ForEach loop.
Add Edition entity instances to the ArrayList editions:
for (Object edition : q.getResultList()) {
editions.add((Edition)edition);
}
Return the ArrayList constructed from the getAllEditions method, which is listed
as follows:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Edition> getAllEditions() {
ArrayList<Edition> editions = new ArrayList<Edition>();
Query q = em.createNamedQuery("findEditionsAll");
for (Object edition : q.getResultList()) {
editions.add((Edition)edition);
}
return editions;
}}
When the getAllEditions query is executed, it returns a List of type <Edition> .
Similarly, define methods getAllArticles and getAllSections . We also need to
add get<>ById methods to retrieve entity instances by ID. Annotate the get<>ById
methods with the @TransactionAttribute with TransactionAttributeType.
REQUIRES_NEW . The get<>ById methods return the corresponding entity instance.
For example, the getEditionById returns an Edition object. The getEditionById
method takes a String argument for the ID. Create a Query object from the named
query findEditionById , which is defined in the Edition entity:
Query q = em.createNamedQuery("findEditionById");
Set the Query object parameter id using the setParameter method:
q.setParameter("id", id);
Retrieve the Edition entity instance from the Query object using the
getSingleResult method:
Edition edition = (Edition)q.getSingleResult();
Return the Edition entity instance from the getEdiitonById method, which is
listed as follows:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Edition getEditionById(String id) {
Query q = em.createNamedQuery("findEditionById");
 
Search WWH ::




Custom Search