2016-05-12 196 views
0

我有2個類。休眠雙級標準

Occupation.class

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

    /** 
    * omitted the fields for brevity 
    */ 

    private int idOccupation; 
    private Posto posto; 

@ManyToOne 
    @JoinColumn(name = "id_posto") 
    public Posto getPosto() { 
     return this.posto; 
    } 

    public void setPosto(Posto posto) { 
     this.posto = posto; 
    } 

而且Posto.class

@Entity 
@Table(name = "posti") 
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="idPosto") 
public class Posto implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int idPosto; 
    private Piano piano; 


    @ManyToOne 
    @JoinColumn(name="id_piano") 
    @JsonIgnore 
    public Piano getPiano() { 
     return piano; 
    } 

    public void setPiano(Piano piano) { 
     this.piano = piano; 
    } 

現在,我想檢索所有職業的對象爲給定的鋼琴

我在做這樣

Criteria criteria = sess.getSession().createCriteria(Occupation.class). 
          createCriteria("posto"). 
     add(Restrictions.eq("piano", piano)); 

     return criteria.list(); 

但我得到這個例外

org.hibernate.QueryException: could not resolve property: posto of: it.besmart.models.Posto 

但它的奇怪,因爲posto應該被稱爲職業,不Posto的本身......

回答

0

更改代碼

Criteria criteria = sess.getSession().createCriteria(Occupation.class). 
          createCriteria("posto"). 
     add(Restrictions.eq("piano", piano)); 

Criteria criteria = sess.getSession().createCriteria(Occupation.class). 
     add(Restrictions.eq("piano", piano)); 

希望它會有所幫助。

+0

謝謝,但它不工作,因爲'piano'it不是Occupation.class'org.hibernate作爲的屬性.QueryException:無法解析屬性:鋼琴:it.besmart.models.Occupation' – besmart

0

我終於設法解決這個問題,很容易與createAlias做...

Criteria criteria = sess.getSession().createCriteria(Occupation.class). 
          createAlias("posto", "p"). 
     add(Restrictions.eq("p.piano", piano));