无法使用本机SQL查询解析属性hibernate

时间:2016-01-12 08:08:53

标签: java spring hibernate

我在使用hibernate模板创建查询时遇到问题。我看了很多教程,然后创建了一个看起来像这样的查询

List fids = getHibernateTemplate().execute(new HibernateCallback<List>() {
        @Override
        public List doInHibernate(Session session) throws HibernateException {
            Query query = session.createQuery(
                    "SELECT DISTINCT m.fournisseurs_id FROM Medicamentfournisseur as m, Composantcommandeclient as c WHERE c.medicamentsFournisseurs_id= m.id AND c.commandeclients_id = :id"
            );
            query.setParameter(":id", id);
            return query.list();
        }
    });

我在SQL控制台中尝试查询它可以工作,但在我的应用程序中我收到此错误:

error :could not resolve property: fournisseurs_id of: com.project.caritas.model.Medicamentfournisseur [SELECT DISTINCT m.fournisseurs_id FROM com.project.caritas.model.Medicamentfournisseur as m, com.project.caritas.model.Composantcommandeclient as c WHERE c.medicamentsFournisseurs_id= m.id AND c.commandeclients_id = :id]; nested exception is org.hibernate.QueryException: could not resolve property: fournisseurs_id of: com.project.caritas.model.Medicamentfournisseur [SELECT DISTINCT m.fournisseurs_id FROM com.project.caritas.model.Medicamentfournisseur as m, com.project.caritas.model.Composantcommandeclient as c WHERE c.medicamentsFournisseurs_id= m.id AND c.commandeclients_id = :id]

有我的POJO

@Entity
@Table(name = "medicamentfournisseur", catalog = "salama")
public class Medicamentfournisseur implements java.io.Serializable {

private Integer id;
private Fournisseur fournisseur;
private double prix;
private String designation;
private String laboratoire;
private String datePeremption;
private String tva;
private Integer disponible;
private Set composantcommandeclients = new HashSet(0);

public Medicamentfournisseur() {
}

public Medicamentfournisseur(Fournisseur fournisseur, double prix, String datePeremption) {
    this.fournisseur = fournisseur;
    this.prix = prix;
    this.datePeremption = datePeremption;
}

public Medicamentfournisseur(Fournisseur fournisseur, double prix, String designation, String laboratoire, String datePeremption, String tva, Integer disponible, Set composantcommandeclients) {
    this.fournisseur = fournisseur;
    this.prix = prix;
    this.designation = designation;
    this.laboratoire = laboratoire;
    this.datePeremption = datePeremption;
    this.tva = tva;
    this.disponible = disponible;
    this.composantcommandeclients = composantcommandeclients;
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fournisseurs_id", nullable = false)
public Fournisseur getFournisseur() {
    return this.fournisseur;
}

public void setFournisseur(Fournisseur fournisseur) {
    this.fournisseur = fournisseur;
}

@Column(name = "prix", nullable = false, precision = 22, scale = 0)
public double getPrix() {
    return this.prix;
}

public void setPrix(double prix) {
    this.prix = prix;
}

@Column(name = "designation", length = 200)
public String getDesignation() {
    return this.designation;
}

public void setDesignation(String designation) {
    this.designation = designation;
}

@Column(name = "laboratoire", length = 200)
public String getLaboratoire() {
    return this.laboratoire;
}

public void setLaboratoire(String laboratoire) {
    this.laboratoire = laboratoire;
}

@Column(name = "datePeremption", nullable = false, length = 200)
public String getDatePeremption() {
    return this.datePeremption;
}

public void setDatePeremption(String datePeremption) {
    this.datePeremption = datePeremption;
}

@Column(name = "tva", length = 50)
public String getTva() {
    return this.tva;
}

public void setTva(String tva) {
    this.tva = tva;
}

@Column(name = "disponible")
public Integer getDisponible() {
    return this.disponible;
}

public void setDisponible(Integer disponible) {
    this.disponible = disponible;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "medicamentfournisseur")
@JsonIgnore
public Set getComposantcommandeclients() {
    return this.composantcommandeclients;
}

public void setComposantcommandeclients(Set composantcommandeclients) {
    this.composantcommandeclients = composantcommandeclients;
}

}

如果有人能解释我如何解决这个问题。

PS:抱歉我的英文不好

1 个答案:

答案 0 :(得分:1)

你应该使用:

SELECT DISTINCT m.fournisseurs FROM Medicamentfournisseur as m ...

因为这实际上不是sql,所以它是一个像sql一样的Hiber的HQL。您应该在java中使用成员名,而不是表列名。