我正在使用Struts2和Hibernate,我收到了这个错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 61 [SELECT s.codeOracle FROM sites as s INNER JOIN contrat as c on c.idSite = s.idSite INNER JOIN paiement as p on p.idcontrat = c.idcontrat INNER JOIN periodepay as pp on pp.idPaiement = p.idPaiement WHERE pp.statutPay = 1 group by s.codeOracle]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
我有这个方法:
@Override
public List<Sites> listSearch() {
List<Sites> site = null;
String query="SELECT s.codeOracle FROM sites as s INNER JOIN contrat as c on c.idSite = s.idSite INNER JOIN Paiement as p on p.idcontrat = c.idcontrat INNER JOIN PeriodePay as pp on pp.idPaiement = p.idPaiement WHERE pp.statutPay = 1 group by s.codeOracle";
try {
site = session.createQuery(query).list();
} catch (Exception e) {
e.printStackTrace();
}
return site;
}
folling代码定义了class:
之间的关系@Entity
@Table(name="Sites")
public class Sites {
private Long idSite;
private Set<Contrat> contratMaping;
@Id
@GeneratedValue
@Column(name="idSite")
public Long getIdSite() {
return idSite;
}
@OneToMany(mappedBy = "siteMaping")
public Set<Contrat> getContratMaping() {
return contratMaping;
}
}
康特拉特班:
@Entity
@Table(name = "Contrat")
public class Contrat{
private Long idcontrat;
private Bailleur bailleurMaping;
private Sites siteMaping;
private Set<Paiement> paiementMap;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getIdcontrat() {
return idcontrat;
}
@OneToMany(mappedBy = "contratMaping")
public Set<Paiement> getPaiementMap() {
return paiementMap;
}
@ManyToOne
@JoinColumn(name = "idBailleur")
public Bailleur getBailleurMaping() {
return bailleurMaping;
}
@ManyToOne
@JoinColumn(name = "idSite")
public Sites getSiteMaping() {
return siteMaping;
}
}
Paiement class:
@Entity
@Table(name="Paiement")
public class Paiement {
private Long idPaiement;
private Contrat contratMaping ;
private Set<PeriodePay> periodePayMap;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idPaiement")
public Long getIdPaiement() {
return idPaiement;
}
@OneToMany(mappedBy = "paimentMaping")
public Set<PeriodePay> getPeriodePayMap() {
return periodePayMap;
}
@ManyToOne
@JoinColumn(name = "idcontrat")
public Contrat getContratMaping() {
return contratMaping;
}
}
PeriodePay类:
@Entity
@Table(name="PeriodePay")
public class PeriodePay {
private Long idPeriodePay;
private Paiement paimentMaping ;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idPeriodePay")
public Long getIdPeriodePay() {
return idPeriodePay;
}
@ManyToOne
@JoinColumn(name = "idPaiement")
public Paiement getPaimentMaping() {
return paimentMaping;
}
}
这个查询在phpmyadmin中正常工作; 请问你能告诉我错误的
Edite 1:
我正在尝试我没有收到错误:
public List<Object[]> listSearch() {
List<Object[]> site = null;
try {
site = session.createSQLQuery("Select * FROM sites as s " +
"INNER JOIN contrat as c on c.idSite = s.idSite " +
"INNER JOIN paiement as p on p.idcontrat = c.idcontrat " +
"INNER JOIN periodepay as pp on pp.idPaiement = p.idPaiement " +
"WHERE pp.statutPay = 1 group by s.codeOracle").list();
for(Object[] arr : site){
System.out.println("Select * FROM sites "+Arrays.toString(arr));
}
} catch (Exception e) {
e.printStackTrace();
}
return site;
}
我在控制台中得到结果:
INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Hibernate: Select * FROM sites as s INNER JOIN contrat as c on c.idSite = s.idSite INNER JOIN paiement as p on p.idcontrat = c.idcontrat INNER JOIN periodepay as pp on pp.idPaiement = p.idPaiement WHERE pp.statutPay = 1 group by s.codeOracle
Select * FROM sites [1, Agadir, , , Aga-1212, Aga-1212, 2015-07-05, , , , false, , , , , 1, 7, 100.0, 2016-01-01, 2015-07-01, 2017-06-30, , 440.0, , , null, , 0.0, 1, 1, null, null, null, null, null, null, null, null, null, null, null, null, null, 13, null, null, null, null, null, null, , , testhamza, , null, , , 2, 7, 28, null, 2640.0, 0.0, true, 13, 2016-01-01, 2017-06-30]
但是使用jsp我不会在我的数据表中得到结果,也许我必须修复迭代器
<s:if test="searchsiteList.size() > 0">
<div id="mbox_inbox">
<table class="table table-striped table-condensed" id="dt_b">
<thead>
<tr>
<th>CodeOracle</th>
<th>CodeGSM</th>
<th>Area</th>
<th>Date MiseOnAir</th>
</tr>
</thead>
<tbody>
<s:iterator value="searchsiteList" status="userStatus">
<tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="codeOracle" /></td>
<td><s:property value="codeGSM" /></td>
<td><s:property value="area" /></td>
<td><s:property value="dateMiseOnAir" /></td>
</tr>
</s:iterator>
</tbody>
</table>
</div>
</s:if>
答案 0 :(得分:1)
It's a HQL syntax error. HQL works with objects and have a different syntax rather than SQL. If you want to execute SQL query, then you should use
session.createSQLQuery(query)
the list()
method returns List<Object[]>
.
In the action class you have
private List<Object[]> searchsiteList = new ArrayList<Object[]>();
public List<Object[]> getSearchsiteList() { return searchsiteList; }
but this code only binds searchsiteList
in JSP. To bind other properties you should create a bean or use a map.
public List<Map<String, Object>> convertToMapsList(List<Object[]> list){
List<Map<String, Object>> res = new ArrayList<>();
for(Object[] arr : list){
Map<String, Object> map = new HashMap<>();
map.put("codeOracle", arr[0]);
map.put("codeGSM", arr[1]);
map.put("area", arr[2]);
map.put("dateMiseOnAir", arr[3]);
res.add(map);
}
return res;
}
and change
public List<Map<String, Object>> getSearchsiteList() { return convertToMapsList(searchsiteList); }
答案 1 :(得分:0)
The create query returns a Query object. Try with below code.
Query query = session.createQuery(hql);
List results = query.list();
//join examples
query = session.createQuery("select e.name, a.city from Employee e "
+ "INNER JOIN e.address a");
List<Object[]> list = query.list();
for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
Your List is of type <Sites>
. This may be causing issue.
Refer:
http://www.tutorialspoint.com/hibernate/hibernate_query_language.htm
http://www.journaldev.com/2954/hibernate-query-language-hql-example-tutorial