Featured Post

Event Sourcing Video from Michael Ploed

Event Sourcing I want to share a great video I found few days ago that describes very well what Event Sourcing is.

Tuesday, May 26, 2015

JPA Example of Named Query


@NamedQueries(value = @NamedQuery(name = "findContactByMail", query = "SELECT c FROM Contact c WHERE c.mail= :mail AND c.location= :location"))
@Table(name = "CONTACT")
public class Contact {

 @Column(name = "ID")
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 @Column(name = "MAIL")
 private String mail;

 @Column(name = "LOCATION")
 private String location;



   private EntityManager entityManager;

   public Contact findContactByMail(String mail, String location){

     TypedQuery findContactByMail = entityManager.createNamedQuery("findContactByMail",Contact.class);
     findContactByMail.setParameter("mail", mail);
     findContactByMail.setParameter("location", location);

       try {
            return findContactByMail.getSingleResult();
       catch (NoResultException e) {
            return null;

No comments :

Post a Comment