0 Replies Latest reply on May 20, 2008 8:11 PM by Matt Davis

    Simple query always empty

    Matt Davis Newbie

      Hello, I'm developing an application based on some simple examples from Yuan's seam book and auto-generated code. I posted this on a Hibernate forum, but y'all and the Hibernate crowd have a tendency to point at each other when there's a problem, so I'm trying both :)

      I've created an Entity Bean named EmailGather, which holds a simple string (email address) and a boolean.

      I have a JSF/Seam xhtml page that gathers the info from the user, and puts it into the database.

      This works great, so I know that my bean is mapped to my table correctly.

      I want to query before I add a new entry, to see if there is already an entry with the same email address.

      My problem is that the query never returns results. I'm not sure what it's querying, exactly, and if it ever actually looks at the tables in my database.

      I am basing my query off of this (It was originally seam auto-generated code):

      List cats = em.createQuery(
          "select cat from Cat as cat where cat.birthdate < ?1")
          .setParameter(1, date, TemporalType.DATE)

      I am not sure how much case matters, or exactly which fields I am referring to in this query. Am I referencing the Entity bean's fields when I say select emailAddress or is that referring to the table?

      And here is my query:

      Query q = em.createQuery("select emailAddress from EmailGather as e where e.emailAddress=?1").setParameter(1, gatheredEmail.getEmailAddress());
            List existing = q.getResultList();
            if (existing.size() != 0) {

      List existing is always of size 0, even though there is an entry in the database with the same email address. I used the debugger and verified that the query was receiving the argument correctly... the email address that comes from the entity bean is buried down in the hash table it uses to store arguments.

      It appears to execute correctly because the JBoss Console says this:

      12:06:03,682 INFO  [STDOUT] Hibernate:
              emailgathe0_.emailaddress as col_0_0_
              emailgather emailgathe0_

      My entity bean:

      package org.domain.EmailGather2.entity;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      public class EmailGather {
         private long id;
         private String emailAddress;
         private Boolean allUpdates;
         public EmailGather(){
         public EmailGather(String emailAddress, Boolean allUpdates){
            this.emailAddress = emailAddress;
            this.allUpdates = allUpdates;
         @Id @GeneratedValue(strategy=GenerationType.AUTO)
         public long getId() {System.out.println("gothere3");return id;}
         public void setId(long id) {System.out.println("gothere4");this.id = id;}
         public String getEmailAddress() {
            return emailAddress;
         public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
         public Boolean getAllUpdates() {
            return allUpdates;
         public void setAllUpdates(Boolean allUpdates) {
            this.allUpdates = allUpdates;

      Full stateless session bean:

      package org.domain.EmailGather2.session;
      import java.util.List;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;
      import org.domain.EmailGather2.entity.EmailGather;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.faces.FacesMessages;
      public class EmailGatherActionBean implements EmailGatherAction {
          @Logger private Log log;
          FacesMessages facesMessages;
          @In(value="emailgatherer", required=true)
          private EmailGather gatheredEmail;
          private EntityManager em;
          public String emailGatherAction() {
             Query q = em.createQuery("select emailAddress from EmailGather as e where e.emailAddress=?1").setParameter(1, gatheredEmail.getEmailAddress());
            List existing = q.getResultList();
            if (existing.size() != 0) {
               em.remove(gatheredEmail); // never gets called
            log.info("Gathered email address:#{gatheredEmail.emailaddress}. Add to mailing list:#{gatheredEmail.allupdates}");
            if (gatheredEmail.getAllUpdates() == true) {
               FacesMessages.instance().add("Thanks! blah blah1.");
            } else {
               FacesMessages.instance().add("Thanks! blah blah2");
            return "/home.xhtml";

      And my JSF code:

      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           <ui:define name="body">
          <h:form id="emailgather" styleClass="edit">
                  <f:facet name="header"></f:facet>
               <s:decorate id="emailaddressDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Email Address</ui:define>
                      <h:inputText id="emailaddress"
                  <s:decorate id="allupdatesDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Add me to your mailing list</ui:define>
                       <h:selectBooleanCheckbox id="allupdates"
                       <h:outputLabel for="allupdates">
                          <h:outputText id="allupdatesLabel" value="(You'll receive all our interesting updates, not just our launch announcement)" />
                  <div style="clear:both">
                      <span class="required">*</span>
                      required fields
                 <div class="actionButtons">
                    <h:commandButton type="submit" value="Sign me up!" action="#{emailGatherAction.emailGatherAction}"/>
                  <h:messages globalOnly="true" styleClass="message"/>

      Table description in Postgres:

      Table name: emailgather
      Column         |Type                   |Modifiers
      emailaddress   character(256)      not null
      allupdates        boolean                not null
      id                    integer                  not null default nextval('emailgather_id_seq'::regclass)
          "emailgather_pkey" PRIMARY KEY, btree (id)

      Any advice is appreciated!