关于SSH多表联查sql语句中jion和Path expected for join!的问题!

       我的查询总共四个表:发票总表IV_Invoice 入库表IV_InInvoice  出库表IV_OutInvoice  交票表IV_Archive,然后对应关系就省略了。。。关系搭建的是没有任何问题的...直接看查询代码

//查询总表IV_InvoiceVo这个类是我查的所有数据封装类
	public List findall() {
			// TODO Auto-generated method stub
		System.out.println("查询总表daoimpl进来了。。。。");
		List list=getSession().createQuery("select new com.ssh.entity.IV_InvoiceVo(a.InvoiceType,b.StartNo,b.EndNo,b.Counts AS Sums,c.Counts,(b.Counts-c.Counts)AS Wsum,(SELECT COUNT(*) FROM IV_Invoice  WHERE Used=1)AS Used,(SELECT COUNT(*) FROM IV_Invoice  WHERE Invalid=1)AS Invalid,(d.counts) AS Archived,(c.Counts-d.counts)AS Wjcount,b.InDate)  FROM IV_Invoice AS a inner JOIN IV_INInvoice AS b ON a.InInvoiceID=b.InInvoiceID inner JOIN IV_OutInvoice AS c ON b.InInvoiceID=c.InInvoiceID inner JOIN iv_archive AS d ON d.InInvoiceID=a.InInvoiceID").list();
		return list;
		}

然后看IV_InvoiceVo类中类型(这个类是我把4张表所查的数据封装在里边了 就是查询语句as 的那些别名那些)

package com.ssh.entity;

import java.util.Date;

public class IV_InvoiceVo {
private int InvoiceType;//发票类型
	private String StartNo;//开始号码
	private String EndNo;//结束号码
	private int Sums;//总张数
	private int  Counts;//出库数
	private int Wsum;//未出数
	private int Used;//已用
	private int Invalid;//作废
	private int Archived;//已交
	private int Wjcount;//未交
	private Date InDate;//入库时间
	//private String EmpName;//入库人
     public IV_InvoiceVo() {
	}
//...省略构造方法
}

jsp页面代码使用的s标签页面过于太长剪短至循环


						
							发票类型
							起始号码
							终止号码
							总张数
							出库数
							未出数
							已用数
							作废数
							待用数
							已交数
							未交数
							入库日期
							入库操作员
						
					
					
						
						
						
						普通发票
						增值税发票
						
							
							
							
							
							
							
							
							
							
							
							
							${session.empName}
							
						
						
						
					

然后再看报错

08:51:09.068 [http-nio-8080-exec-3] ERROR org.hibernate.hql.internal.ast.ErrorCounter -  Invalid path: 'b.StartNo'
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'b.StartNo'
----
----
----
08:51:09.079 [http-nio-8080-exec-3] ERROR org.hibernate.hql.internal.ast.ErrorCounter -  Invalid path: 'b.Counts'
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'b.Counts'
----
----
----
08:51:09.080 [http-nio-8080-exec-3] ERROR org.hibernate.hql.internal.ast.ErrorCounter - :0:0: unexpected end of subtree
antlr.NoViableAltException: unexpected end of subtree

它说我这个b别名没有但是我在数据库中实现这条查询语句是可以执行的

 

select  a.InvoiceType,b.StartNo,b.EndNo,b.Counts AS Sums,c.Counts,(b.Counts-c.Counts)AS Wsum,(SELECT COUNT(*) FROM IV_Invoice  WHERE Used=1)AS Used,(SELECT COUNT(*) FROM IV_Invoice  WHERE Invalid=1)AS Invalid,(d.counts) AS Archived,(c.Counts-d.counts)AS Wjcount,b.InDate,e.EmpName  FROM IV_Invoice AS a inner JOIN IV_INInvoice AS b ON a.InInvoiceID=b.InInvoiceID inner JOIN IV_OutInvoice AS c ON b.InInvoiceID=c.InInvoiceID inner JOIN iv_archive AS d ON d.InInvoiceID=a.InInvoiceID inner JOIN SY_Emp AS e ON a.Empid=e.Empid

关于SSH多表联查sql语句中jion和Path expected for join!的问题!_第1张图片

然后报那个Bug我仔细改又是找的,然后终于不报这个错了但是又换了一个错mmp...

Path expected for join!

然后我百度了看了这个博主的https://blog.csdn.net/sinat_36707441/article/details/79421313

说hql不支持join语法。

我的查询语句他是自动生成的,自动生成的语句不太能写编写复杂的,我也不确定。。。。

后面我继续弄,终于解决了这个问题...

1:实体类类型转换定义错了,定义的别名要用long型去封装。有些可能也不一样...

 

	private long Used;//已用
	private long Invalid;//作废
//查询总表
public List findall() {
			// TODO Auto-generated method stub
		//System.out.println("查询总表daoimpl进来了。。。。");
		List list=getSession().createQuery("select new com.ssh.entity.IV_InvoiceVo(a.InvoiceType,b.StartNo,b.EndNo,b.Counts as Sums,c.Counts,(b.Counts-c.Counts)as Wsum,(SELECT COUNT(*) FROM IV_Invoice  WHERE Used=1)as Used,(SELECT COUNT(*) FROM IV_Invoice  WHERE Invalid=1)as Invalid,(d.Counts) as Archived,(c.Counts-d.Counts)as Wjcount,b.InDate)  FROM IV_Invoice a,IV_InInvoice b inner JOIN IV_OutInvoice AS c ON b.InInvoiceID=c.InInvoiceID inner JOIN IV_Archive AS d ON d.InInvoiceID=a.InInvoiceID  and  a.InInvoiceID=b.InInvoiceID ").list();
		return list;
		}

2:关于你建了关系的表要联合起来。:

列如:FROM IV_Invoice a,IV_InInvoice b and a.id=b.id inner join.......

然后还有就是你搭建的关系可能像我这个一样empid在实体类中改为对象了,然后我把它删了,自己

session里调


//	private String EmpName;//入库操作人

3:没有解决不了Bug

事实证明自动生成sql还是可以编译复杂点的语句。。

总结Bug解决方法

1.hql的join不支持没使用外键连接的不同表查询

2.查询语句是你的实体类名称不是数据库里的

3.搭建关系的表要联合

4.实体类里面的你定义的类型要跟sql一样

5.还有就是你多方关系可以选择像我一样session调。哈哈哈

 

 

 

 

你可能感兴趣的:(SSH整合)