import com.apress.prospring3.ch10.domain.Contact;
public interface ContactService {
// Find all contacts
public List<Contact> findAll();
// Find all contacts with telephone and hobbies
public List<Contact> findAllWithDetail();
// Find a contact with details by id
public Contact findById(Long id);
// Insert or update a contact
public Contact save(Contact contact);
// Delete a contact
public void delete(Contact contact);
}
The interface is very simple; it has just three finder methods, one save method, and one delete
method. The save method will serve both the insert and update operations.
Query Data Using the Java Persistence Query Language
The syntax for JPQL and HQL is very similar, and in fact, all the HQL queries that we used in Chapter 9
are reusable to implement the three finder methods within the ContactService interface.
Let's recap the named queries defined for the Contact entity class. Listing 10-5 shows the code
snippet.
Listing 10-5. Named Queries for the Contact Domain Object
package com.apress.prospring3.ch10.domain;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "contact")
@NamedQueries({
@NamedQuery(name="Contact.findAll", query="select c from Contact c"),
@NamedQuery(name="Contact.findById",
query="select distinct c from Contact c left join fetch c.contactTelDetails t left
join fetch c.hobbies h where c.id = :id"),
@NamedQuery(name="Contact.findAllWithDetail",
query="select distinct c from Contact c left join fetch c.contactTelDetails t left
join fetch c.hobbies h")
})
public class Contact implements Serializable {
// Other code omitted
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home