问题描述
$ b
我正在使用Struts2和Hibernate,并且出现此错误: >
org.hibernate.hql.ast.QuerySyntaxException:意外标记:在第1行第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 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)
我有这种方法:
$ b
@覆盖
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 pp。 idPaiement = p.idPaiement WHERE pp.statutPay = 1 group by s.codeOracle;
尝试{
site = session.createQuery(query).list();
} catch(Exception e){
e.printStackTrace();
}
返回网站;
}
folling代码定义类之间的关系:
@Entity
@Table(name =Sites)
public class Sites {
私人长途ID网站;
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 {
私人长期idcontrat;
私人Bailleur bailleurMaping;
私人网站sitemaping;
私人套装< 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 {
私有长idPaiement;
private Contrat contratmaping;
私人套餐< PeriodePay> periodePayMap;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =idPaiement)
public Long getIdPaiement(){
返回idPaiement;
}
@OneToMany(mappedBy =paimentMaping)
public Set< PeriodePay> getPeriodePayMap(){
返回periodePayMap;
@ManyToOne
@JoinColumn(name =idcontrat)
public Contrat getContratMaping(){
return contratMaping;
PeriodePay类:
@Entity
@Table(name =PeriodePay)
public class PeriodePay {
私人长期idPeriodePay;
私人Paiment paimentMaping;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =idPeriodePay)
public Long getIdPeriodePay(){
return idPeriodePay;
}
@ManyToOne
@JoinColumn(name =idPaiement)
public Paiement getPaimentMaping(){
return paimentMaping;
$ b $ p
$ b 这个查询在phpmyadmin中工作正常;
请你可以告诉我什么是错误的
Edite 1:
我正在尝试我不会收到错误:
public List< Object []> listSearch(){
List< Object []> site = null;
尝试{
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 pp.idPaiement = p.idPaiement+
where pp.statutPay = 1 group by s。 codeOracle)名单()。
for(Object [] arr:site){
System.out.println(Select * FROM sites+ Arrays.toString(arr));
}
} catch(Exception e){
e.printStackTrace();
}
返回网站;
}
我在控制台中得到结果:
INFOS:至少有一个JAR针对TLD进行了扫描,但尚未包含任何TLD。为此记录器启用调试日志记录以获取已扫描但未找到TLD的JAR的完整列表。在扫描过程中跳过不需要的JAR可以缩短启动时间并缩短JSP编译时间。
Hibernate:选择* FROM网站作为s INNER JOIN对照c.idSite = s.idSite INNER JOIN paiement作为p on p.idcontrat = c.idcontrat INNER JOIN periodepay as pp pp.idPaiement = p。 idPaiement WHERE pp.statutPay = 1 group by s.codeOracle
选择* 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 null null null null null null testhamza null null 2,7 28 null null 2640.0 0.0是true ,13,2016-01-01,2017-06-30]
但是使用jsp我don' t得到我的数据表中的结果,也许我必须修复迭代器
$ b < s:如果test =searchsiteList.size()> 0>
< div id =mbox_inbox>
< table class =table table-striped table-condensedid =dt_b>
< thead>
< tr>
CodeOracle< / th>
CodeGSM< / th>
< th>区域< / th>
< th> Date MiseOnAir< / th>
< / tr>
< / thead>
< tbody>
< s:iterator value =searchsiteListstatus =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:属性值=codeGSM/>< / td>
< td>< s:属性值=区域/>< / td>
< td>< s:属性值=dateMiseOnAir/>< / td>
< / tr>
< / s:iterator>
< / tbody>
< / table>
< / div>
< / s:if>
解决方案这是一个HQL语法错误。 HQL与对象一起工作,并且具有不同的语法而不是SQL。如果你想执行SQL查询,那么你应该使用
session.createSQLQuery(query)
list()
方法返回列表与LT;对象[]>
。
在动作类中,您有
private List< Object [ ] GT; searchsiteList = new ArrayList< Object []>();
public List< Object []> getSearchsiteList(){return searchsiteList; }
但这段代码只绑定 searchsiteList
in JSP。要绑定其他属性,您应该创建一个bean或使用一个映射。
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;
}
并更改
public List< Map< String,Object>> getSearchsiteList(){return convertToMapsList(searchsiteList); }
I'm working with Struts2 and Hibernate and I'm getting this error :
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)
i have this method :
@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;
}
the folling code define the relationship between 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;
}
}
Contrat class :
@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 class :
@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;
}
}
this query work fine in phpmyadmin;Please can you tell me what the wrong
Edite 1 :
i'm trying that i'don't get the error :
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;
}
i get the result in the console :
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]
But using jsp i don't get the result in my datatable ,maybe i have to fix the iterator
<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>
解决方案 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); }
这篇关于org.hibernate.hql.ast.QuerySyntaxException:意外的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!