gpt4 book ai didi

java - 无法使用 native SQL 查询解析属性 hibernate

转载 作者:行者123 更新时间:2023-12-01 10:35:53 25 4
gpt4 key购买 nike

我在使用 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:抱歉我的英语不好

最佳答案

你应该使用:

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

因为这实际上不是一条sql,它是一个类似hibernate的sql的HQL。您应该使用java中的成员名称,而不是表列名称。

关于java - 无法使用 native SQL 查询解析属性 hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com