JPA - 使用Criteria内部联接表和复合键发出问题

时间:2012-09-24 23:15:26

标签: java hibernate jpa composite-key hibernate-criteria

您好我尝试使用Criteria进行此查询:

    select p.idpregled,p.idkarton,p.datum_pregleda,k.status from pregled p 
 inner join karton k 
 on p.idkarton = k.idkarton 
 where k.status ='Otvoren';

这两个表之间的关系是这样的:

table karton
idkarton (pk)
..
..
table pregled
idpregled (pk)
idkarton (pk,fk)
.
.

这两个表中的两个代码:

@Entity
@Table(name ="karton")
public class Karton implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Id
    private KartonPrimaryKey pk = new KartonPrimaryKey();

    @Column(name ="status", nullable = false)
    private String status;
    @Column(name="datum_otvaranja" , nullable = false)
    private Date datumOtvaranja;
    @Column(name = "napomena")
    private String napomena;
    @ManyToOne(targetEntity = Pacijent.class)
    @JoinColumn(name ="idpacijent",nullable = false,unique = true)
    private Pacijent pacijent;

    @OneToMany(mappedBy = "pk.kartonForeignKey")
    @GwtTransient
    private List<Pregled> pregledi = new LinkedList<Pregled>();




    public KartonPrimaryKey getPk() {
        return pk;
    }
    public void setPk(KartonPrimaryKey pk) {
        this.pk = pk;
    }

    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public Date getDatumOtvaranja() {
        return datumOtvaranja;
    }
    public void setDatumOtvaranja(Date datumOtvaranja) {
        this.datumOtvaranja = datumOtvaranja;
    }
    public String getNapomena() {
        return napomena;
    }
    public void setNapomena(String napomena) {
        this.napomena = napomena;
    }
    public Pacijent getPacijent() {
        return pacijent;
    }
    public void setPacijent(Pacijent pacijent) {
        this.pacijent = pacijent;
    }
    public List<Pregled> getPregledi() {
        return pregledi;
    }
    public void setPregledi(List<Pregled> pregledi) {
        this.pregledi = pregledi;
    }



      public int hashCode(){
          final int prime = 31;
          int result = 1;
          result = prime * result
                  +((pregledi == null) ? 0 : pregledi.hashCode());
          result = prime * result
                  +((pk == null) ? 0 : pk.hashCode());
          return result;
      }

      public boolean equals(Object obj){
          if (this == obj) 
            return true;
          if (obj == null) 
            return false;
          if(getClass() != obj.getClass())
              return false;
          final Karton other = (Karton) obj;
          if (pregledi == null) {
            if(other.pregledi != null)
                return false;
          }else if(!pregledi.equals(other.pregledi))
              return false;
          if (pk == null) {
            if(other.pk != null)
                return false;
        }else if(!pk.equals(other.pk))
            return false;
          return true;
      }


}

@Embeddable
public class KartonPrimaryKey implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;



    @Column(name ="idkarton")
     Integer idKarton;





    public Integer getIdKarton() {
        return idKarton;
    }





    public void setIdKarton(Integer idKarton) {
        this.idKarton = idKarton;
    }





    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((idKarton == null) ? 0 : idKarton.hashCode());
//        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }


    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(getClass() != obj.getClass())
            return false;
        final KartonPrimaryKey other = (KartonPrimaryKey) obj;
        if (idKarton == null) {
            if(other.idKarton != null)
                return false;
        }else if(!idKarton.equals(other.idKarton))
            return false;
        return true;
    }

}
@Entity
@Table(name = "pregled")

@AssociationOverrides({
    @AssociationOverride(name = "pk.idPregled", 
        joinColumns = @JoinColumn(name = "idpregled")),
    @AssociationOverride(name = "pk.kartonForeignKey", 
        joinColumns = @JoinColumn(name = "idkarton")) })
public class Pregled implements Serializable {





    /**
     * 
     */
    private static final long serialVersionUID = 1L;



    @EmbeddedId
     PregledKartonId pk = new PregledKartonId();



    @Column(name ="datum_pregleda")
    private Date datumPregleda;

    @ManyToOne(targetEntity = Dijagnoza.class)
    @JoinColumn(name ="iddijagnoza",nullable = false)
    private Dijagnoza dijagnoza;

    @OneToMany(mappedBy = "primaryKey.pregledForeignKey")
    @GwtTransient
    private List<PregledStavke> pregledStavke = new LinkedList<PregledStavke>();


    public Dijagnoza getDijagnoza() {
        return dijagnoza;
    }
    public void setDijagnoza(Dijagnoza dijagnoza) {
        this.dijagnoza = dijagnoza;
    }


    public PregledKartonId getPregledKarton() {
        return pk;
    }

    public void setPregledKarton(PregledKartonId pregledKarton) {
        this.pk = pregledKarton;
    }

    @Transient
    public Integer getIdPregled() {
        return pk.getIdPregled();
    }

    public void setIdPregled(Integer id) {
        pk.setIdPregled(id);
    }





    public PregledKartonId getPk() {
        return pk;
    }
    public void setPk(PregledKartonId pk) {
        this.pk = pk;
    }
    public List<PregledStavke> getPregledStavke() {
        return pregledStavke;
    }
    public void setPregledStavke(List<PregledStavke> pregledStavke) {
        this.pregledStavke = pregledStavke;
    }

    @Transient
    public Karton getKarton() {
        return getPregledKarton().getKartonForeignKey();
    }
    public void setKarton(Karton karton) {
        getPregledKarton().setKartonForeignKey(karton);
    }
    public Date getDatumPregleda() {
        return datumPregleda;
    }
    public void setDatumPregleda(Date datumPregleda) {
        this.datumPregleda = datumPregleda;
    }


    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        Pregled that = (Pregled) o;
        if (pregledStavke == null) {
            if (that.pregledStavke != null) {
                return false;
            }
        }else if(!pregledStavke.equals(that.pregledStavke))
            return false;
        if (pk == null) {
            if (that.pk != null)
                return false;

        }else if (!pk.equals(that.pk))
            return false;


        return true;
    }
// 
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result +((pregledStavke == null) ? 0 : pregledStavke.hashCode());
        result = prime * result +
                ((pk == null) ? 0 : pk.hashCode());
        return result;
    }







}


@Embeddable
public class PregledKartonId implements Serializable {


    @GeneratedValue
    @Column(name = "idpregled")
     Integer idPregled;

    @ManyToOne
    private Karton kartonForeignKey;







    public Integer getIdPregled() {
        return idPregled;
    }

    public Karton getKartonForeignKey() {
        return kartonForeignKey;
    }

    public void setKartonForeignKey(Karton kartonForeignKey) {
        this.kartonForeignKey = kartonForeignKey;
    }

    public void setIdPregled(Integer idPregled) {
        this.idPregled = idPregled;
    }



    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((idPregled == null)? 0 : idPregled.hashCode());
        result = prime + result
                + ((kartonForeignKey == null) ? 0 : kartonForeignKey.hashCode());
        return result;
    }



    public boolean equals(Object o){
        if (this == o) return true;
        if(o == null)
            return false;
        if(getClass() != o.getClass())
            return false;

        final PregledKartonId  that = (PregledKartonId) o;
        if(idPregled == null){
            if(that.idPregled != null)
                return false;
        }else if(!idPregled.equals(that.idPregled))
            return false;
        if(kartonForeignKey == null){
            if(that.kartonForeignKey != null)
                return false;
        }else if(!kartonForeignKey.equals(that.kartonForeignKey))
            return false;



        return true;
    }



}

我尝试了这个标准代码,但这没有成功:

Criteria criteria = session.createCriteria(Pregled.class);
        criteria.setFetchMode("kartonForeignKey", FetchMode.JOIN).add(Restrictions.eq("status", "Otvoren"));
List<Pregled> pregledi = criteria.list();

这段代码在stacktrace中告诉我这个错误:

com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.util.List com.fit.klinika.client.pregled.PregledService.queryPregledOtvoren()' threw an unexpected exception: org.hibernate.QueryException: could not resolve property: status of: com.fit.klinika.client.pregled.Pregled
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

提前感谢:)

当我尝试这个时:

 Criteria criteria = session.createCriteria(Pregled.class,"pregled");
            criteria.createAlias("pregled.pk.kartonForeignKey","karton");

这很好,如果我使用where coditions添加这行代码:

criteria.add(Restrictions.eq("karton", "Otvoren"));

抛出同样的错误???

然后我在google上做了一些关于此的搜索并发现,这个问题没有通过hibernate解决

https://hibernate.onjira.com/browse/HB-417

https://hibernate.onjira.com/browse/HHH-1742

https://hibernate.onjira.com/browse/HHH-1570

这个bug来自2003年,并没有被hibernate修复,这可能吗?wtf?!?!?!?

如果我错了,请纠正我:)......

1 个答案:

答案 0 :(得分:0)

错误消息表明它无法在Pregled类中找到名为“status”的属性。在您的代码示例中,您可以看到Pregled类实际上没有名为“status”的映射字段。