在查询多对多关联时出现Criteria问题

时间:2015-07-18 21:05:32

标签: java hibernate annotations

我有一个关于hibernate如何实现多对多关系的问题。 Hibernate创建了以下2个类来映射表关系:

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类中提到了相关领域:

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

对我错过的任何想法? Thx提前。

1 个答案:

答案 0 :(得分:0)

一个美好的夜晚让我明白了。

我在错误的对象上使用了参数:

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

虽然它应该是这样的:

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

希望它会帮助别人。