0 Replies Latest reply on May 4, 2010 11:26 AM by Alexander Kudryavtsev

    @EmbeddedId Query gives null

    Alexander Kudryavtsev Newbie

      Hello,

       

      I use SEAM - generated (and customized later on) entities. They contain  @EmbeddedId which is suitable because my tables doesn't have unique id as a single column so composite is in use by means of @EmbeddedId / @Embeddable.

       

      The problem is that Query returns null object.

       

      My question - what is the problem that such a query returns null and which solution is applicable??

       

      Thank you in advance

       

      {code}

       

      query = "SELECT t FROM Gm3 t where t.id.apb=:apb";
      final Gm3 gm3;
      try {
      gm3 = (Gm3) entityManager.createQuery(query).setParameter("apb", this.apb).getSingleResult();
      } catch (NoResultException e) {
      throw new RuntimeException("Can't read from GM3. " + query, e);
      } catch (Exception e) {
      throw new RuntimeException("Can't read from GM3. " + query, e);
      } {code}

       

      Entity:

       

      {code}

      @Entity
      @Table(name = "GM3")
      public class Gm3 implements java.io.Serializable {

       

      private Gm3Id id;

       

      public Gm3() {
      }

       

      public Gm3(Gm3Id id) {
      this.id = id;
      }

       

      @EmbeddedId
      @AttributeOverrides({@AttributeOverride(name = "apb",  nullable = true,column = @Column(name = "APB", length = 8)),
      @AttributeOverride(name = "nummer", column = @Column(name = "NUMMER", length = 2)),
      @AttributeOverride(name = "id", column = @Column(name = "ID", length = 10))})
      public Gm3Id getId() {
      return this.id;
      }

       

      public void setId(Gm3Id id) {
      this.id = id;
      }

       

      private String taal;
      private String stars;
      private String national;
      private Date ndatum;
      private Date nstop;
      private String dosis;
      private String n1;
      private String multiply;
      private String prijs;
      private String bprijs;
      private Date pdatum;
      private Date bpdatum;
      private String rg1;
      private String rg2;

      . . .
      {code}

       

      {code}

      @Embeddable
      public class Gm3Id implements java.io.Serializable {

       

      private String apb;
      private String nummer;
      private String id;

       

      public Gm3Id() {
      }

       

      public Gm3Id(String apb, String nummer, String id) {
      this.apb = apb;
      this.nummer = nummer;
      this.id = id;
      }

       

      @Column(name = "APB", length = 8)
      public String getApb() {
      return this.apb;
      }

       

      public void setApb(String apb) {
      this.apb = apb;
      }

       

      @Column(name = "NUMMER", length = 2)
      public String getNummer() {
      return this.nummer;
      }

       

      public void setNummer(String nummer) {
      this.nummer = nummer;
      }

       

      @Column(name = "ID", length = 10)
      public String getId() {
      return this.id;
      }

       

      public void setId(String id) {
      this.id = id;
      }

       

      public boolean equals(Object other) {
      if ((this == other))
      return true;
      if ((other == null))
      return false;
      if (!(other instanceof Gm3Id))
      return false;
      Gm3Id castOther = (Gm3Id) other;

       

      return ((this.getApb() == castOther.getApb()) || (this.getApb() != null && castOther.getApb() != null && this
      .getApb().equals(castOther.getApb())))
      && ((this.getNummer() == castOther.getNummer()) || (this.getNummer() != null
      && castOther.getNummer() != null && this.getNummer().equals(castOther.getNummer())))
      && ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this
      .getId().equals(castOther.getId())));
      }

       

      public int hashCode() {
      int result = 17;
      result = 37 * result + (getApb() == null ? 0 : this.getApb().hashCode());
      result = 37 * result + (getNummer() == null ? 0 : this.getNummer().hashCode());
      return result;
      }

       

      }

      {code}