0

I have an issue with creating a query with hibernate template. I've take a look at many tutorial and i create my query that look like this

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();
        }
    });

I try query in SQL console and it works but in my apps i got this error :

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]

there is my 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;
}

}

If someone can explain me how to solve this.

PS: Sorry for my bad english

3
  • For native SQL you have to use session.createSQLQuery Commented Jan 12, 2016 at 8:14
  • So i can't do this with hibernateTemplate? session.createQuery is in doInHibernate method Commented Jan 12, 2016 at 8:15
  • I have public <T> T execute(HibernateCallback<T> action) on HibernateTemplate, you could do session.createSQLQuery(...) with own HibernateCallback. Commented Jan 12, 2016 at 9:23

1 Answer 1

1

you should use:

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

Because this is not a sql actually, it is a HQL of hibernate like sql. You should use the member name in java but not table column name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your answer. I'll try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.