2015-07-18 61 views
0

我在hibernate如何實現多對多關係方面存在問題。 休眠創建2以下類映射表的關係:在查詢多對多關聯時遇到Criteria問題

package entities; 

// default package 
// Generated Jul 13, 2015 2:58:02 PM by Hibernate Tools 4.0.0 

import javax.persistence.AttributeOverride; 
import javax.persistence.AttributeOverrides; 
import javax.persistence.Column; 
import javax.persistence.EmbeddedId; 
import javax.persistence.Entity; 
import javax.persistence.Table; 

/** 
* CategoriesDuSpectacle generated by hbm2java 
*/ 
@Entity 
@Table(name = "Categories_Du_Spectacle") 
public class CategoriesDuSpectacle implements java.io.Serializable { 

    private CategoriesDuSpectacleId id; 

    public CategoriesDuSpectacle() { 
    } 

    public CategoriesDuSpectacle(CategoriesDuSpectacleId id) { 
     this.id = id; 
    } 

    @EmbeddedId 
    @AttributeOverrides({ 
      @AttributeOverride(name = "spectacleId", column = @Column(name = "Spectacle_Id", nullable = false)), 
      @AttributeOverride(name = "categorieId", column = @Column(name = "Categorie_Id", nullable = false)), 
      @AttributeOverride(name = "duree", column = @Column(name = "Duree")), 
      @AttributeOverride(name = "commentaire", column = @Column(name = "Commentaire")), 
      @AttributeOverride(name = "theme", column = @Column(name = "Theme")), 
      @AttributeOverride(name = "contrainte", column = @Column(name = "Contrainte")) }) 
    public CategoriesDuSpectacleId getId() { 
     return this.id; 
    } 

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

} 

和:

package entities; 

// default package 
// Generated Jul 13, 2015 2:58:02 PM by Hibernate Tools 4.0.0 

import javax.persistence.Column; 
import javax.persistence.Embeddable; 

/** 
* CategoriesDuSpectacleId generated by hbm2java 
*/ 
@Embeddable 
public class CategoriesDuSpectacleId implements java.io.Serializable { 

    private int spectacleId; 
    private int categorieId; 
    private Integer duree; 
    private String commentaire; 
    private String theme; 
    private String contrainte; 

    public CategoriesDuSpectacleId() { 
    } 

    public CategoriesDuSpectacleId(int spectacleId, int categorieId) { 
     this.spectacleId = spectacleId; 
     this.categorieId = categorieId; 
    } 

    public CategoriesDuSpectacleId(int spectacleId, int categorieId, 
      Integer duree, String commentaire, String theme, String contrainte) { 
     this.spectacleId = spectacleId; 
     this.categorieId = categorieId; 
     this.duree = duree; 
     this.commentaire = commentaire; 
     this.theme = theme; 
     this.contrainte = contrainte; 
    } 

    @Column(name = "Spectacle_Id", nullable = false) 
    public int getSpectacleId() { 
     return this.spectacleId; 
    } 

    public void setSpectacleId(int spectacleId) { 
     this.spectacleId = spectacleId; 
    } 

    @Column(name = "Categorie_Id", nullable = false) 
    public int getCategorieId() { 
     return this.categorieId; 
    } 

    public void setCategorieId(int categorieId) { 
     this.categorieId = categorieId; 
    } 

    @Column(name = "Duree") 
    public Integer getDuree() { 
     return this.duree; 
    } 

    public void setDuree(Integer duree) { 
     this.duree = duree; 
    } 

    @Column(name = "Commentaire") 
    public String getCommentaire() { 
     return this.commentaire; 
    } 

    public void setCommentaire(String commentaire) { 
     this.commentaire = commentaire; 
    } 

    @Column(name = "Theme") 
    public String getTheme() { 
     return this.theme; 
    } 

    public void setTheme(String theme) { 
     this.theme = theme; 
    } 

    @Column(name = "Contrainte") 
    public String getContrainte() { 
     return this.contrainte; 
    } 

    public void setContrainte(String contrainte) { 
     this.contrainte = contrainte; 
    } 

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

     return (this.getSpectacleId() == castOther.getSpectacleId()) 
       && (this.getCategorieId() == castOther.getCategorieId()) 
       && ((this.getDuree() == castOther.getDuree()) || (this 
         .getDuree() != null && castOther.getDuree() != null && this 
         .getDuree().equals(castOther.getDuree()))) 
       && ((this.getCommentaire() == castOther.getCommentaire()) || (this 
         .getCommentaire() != null 
         && castOther.getCommentaire() != null && this 
         .getCommentaire().equals(castOther.getCommentaire()))) 
       && ((this.getTheme() == castOther.getTheme()) || (this 
         .getTheme() != null && castOther.getTheme() != null && this 
         .getTheme().equals(castOther.getTheme()))) 
       && ((this.getContrainte() == castOther.getContrainte()) || (this 
         .getContrainte() != null 
         && castOther.getContrainte() != null && this 
         .getContrainte().equals(castOther.getContrainte()))); 
    } 

    public int hashCode() { 
     int result = 17; 

     result = 37 * result + this.getSpectacleId(); 
     result = 37 * result + this.getCategorieId(); 
     result = 37 * result 
       + (getDuree() == null ? 0 : this.getDuree().hashCode()); 
     result = 37 
       * result 
       + (getCommentaire() == null ? 0 : this.getCommentaire() 
         .hashCode()); 
     result = 37 * result 
       + (getTheme() == null ? 0 : this.getTheme().hashCode()); 
     result = 37 
       * result 
       + (getContrainte() == null ? 0 : this.getContrainte() 
         .hashCode()); 
     return result; 
    } 

} 

我在法國的領域NAES道歉。

現在,當我試圖抓住所有的實體:

List l = session.createCriteria(CategoriesDuSpectacle.class).list(); 

這工作得很好。

但是當我嘗試添加一個標準:

l = session.createCriteria(CategoriesDuSpectacle.class).add(Restrictions.ilike("commentaire", "a")).list(); 

我收到以下錯誤:

org.hibernate.QueryException: could not resolve property: commentaire of: entities.CategoriesDuSpectacle 

的關注領域是CategoriesDuSpectacle類mentionned這聽起來很奇怪,我說:

@EmbeddedId 
    @AttributeOverrides({ 
     {...} 
      @AttributeOverride(name = "commentaire", column = @Column(name = "Commentaire")), 
     {...} 
}) 

有關我錯過了什麼的任何想法? Thx提前。

回答

0

一個良好的夜晚讓我想通了。

我用錯了對象上的參數:

l = session.createCriteria(CategoriesDuSpectacle.class).add(Restrictions.ilike("commentaire", "a")).list(); 

雖然它應該是這樣:

l = session.createCriteria(CategoriesDuSpectacle.class).add(Restrictions.ilike("id.commentaire", "a")).list(); 

希望這將幫助別人。