HQL语句报错

wuwenzhe 2013-04-17 06:34:14
这句话是我写的HQL查询语句:


String queryString = "select a.id,a.authority.id,a.name,a.description,b.id,b.name from Authority a left outer join Authority b where a.authority.id = b.id and a.authority.id is not null";



下面是报的错误:



803 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1) Path expected for join!
18:31:30,806 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1) Invalid path: 'b.id'
18:31:30,808 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1) Invalid path: 'b.id'
18:31:30,809 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1) right-hand operand of a binary operator was null
18:31:30,809 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1) <AST>:0:0: unexpected end of subtree
18:31:30,810 ERROR [dao.AuthorityDAO] (http--127.0.0.1-8080-1) find all failed: org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [select a.id,a.authority.id,a.name,a.description,b.id,b.name from pojo.Authority a left outer join Authority b where a.authority.id = b.id and a.authority.id is not null]



相同意思的sql语句直接操作数据库

select a.id,a.parentRightID,a.name,b.id,b.name from [wgzxdms].[dbo].[authority] a left join [wgzxdms].[dbo].[authority] b
on a.parentRightID = b.id



就能得到正确的结果
...全文
367 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
多谢各位好朋友们的帮忙,我的问题搞定了 因为配置文件中已经关联的两者,所以直接采用对象导航就可以了,sql语句中的on条件是不用写的,hql智能的加上了,如果有where条件,加上就可以了。以下是我的解决方法。

select a,b from Authority a left join a.authority b
结贴,给分。
babys 2013-04-18
  • 打赏
  • 举报
回复
select a.id,a.name,a.description,b.id,b.name from pojo.Authority as a left join a.authority as b 先试试前面, 再看添加条件 where b.id is not null
十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
既然你都用了hibernate,父对象a.authority可以通过hibernate内部的机制获取,为啥还要自己写HQL?
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 9 楼 fangmingshijie 的回复:
private Authority authority;在这个类中,定义这个对象,有意义吗? 改为: select a.id,a.name,a.description,b.id,b.name from Authority a left join Authority b on a.id = b.id where a.id is not null
貌似hql语句左连接不应该有on出现
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 10 楼 fangmingshijie 的回复:
<many-to-one name="authority" class="pojo.Authority" fetch="join"> <column name="parentRightID" /> </many-to-one> 没看出来你配置这个有啥用
我这个表自关联,我想把有父权限的名字也打印出来
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 10 楼 fangmingshijie 的回复:
<many-to-one name="authority" class="pojo.Authority" fetch="join"> <column name="parentRightID" /> </many-to-one> 没看出来你配置这个有啥用
many-to-one 是one一端配置的,one-to-many是many端配置的,只不过同一张表而已嘛
  • 打赏
  • 举报
回复
<many-to-one name="authority" class="pojo.Authority" fetch="join"> <column name="parentRightID" /> </many-to-one> 没看出来你配置这个有啥用
  • 打赏
  • 举报
回复
private Authority authority;在这个类中,定义这个对象,有意义吗? 改为: select a.id,a.name,a.description,b.id,b.name from Authority a left join Authority b on a.id = b.id where a.id is not null
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 7 楼 fangmingshijie 的回复:
Authority 看下你这个类

public class Authority implements java.io.Serializable {

	// Fields

	private Integer id;
	private Authority authority;
	private String name;
	private String description;
	private Set userAuthorities = new HashSet(0);
	private Set authorities = new HashSet(0);
	private Set roleAuthorities = new HashSet(0);

	// Constructors

	/** default constructor */
	public Authority() {
	}

	/** minimal constructor */
	public Authority(Integer id) {
		this.id = id;
	}

	/** full constructor */
	public Authority(Integer id, Authority authority, String name,
			String description, Set userAuthorities, Set authorities,
			Set roleAuthorities) {
		this.id = id;
		this.authority = authority;
		this.name = name;
		this.description = description;
		this.userAuthorities = userAuthorities;
		this.authorities = authorities;
		this.roleAuthorities = roleAuthorities;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Authority getAuthority() {
		return this.authority;
	}

	public void setAuthority(Authority authority) {
		this.authority = authority;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set getUserAuthorities() {
		return this.userAuthorities;
	}

	public void setUserAuthorities(Set userAuthorities) {
		this.userAuthorities = userAuthorities;
	}

	public Set getAuthorities() {
		return this.authorities;
	}

	public void setAuthorities(Set authorities) {
		this.authorities = authorities;
	}

	public Set getRoleAuthorities() {
		return this.roleAuthorities;
	}

	public void setRoleAuthorities(Set roleAuthorities) {
		this.roleAuthorities = roleAuthorities;
	}

}
  • 打赏
  • 举报
回复
Authority 看下你这个类
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
以下是配置文件,这个表自关联

<hibernate-mapping>
    <class name="pojo.Authority" table="authority" schema="dbo" catalog="wgzxdms">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="authority" class="pojo.Authority" fetch="join">
            <column name="parentRightID" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" />
        </property>
        <set name="userAuthorities" inverse="true">
            <key>
                <column name="rightID" />
            </key>
            <one-to-many class="pojo.UserAuthority" />
        </set>
        <set name="authorities" inverse="true">
            <key>
                <column name="parentRightID" />
            </key>
            <one-to-many class="pojo.Authority" />
        </set>
        <set name="roleAuthorities" inverse="true">
            <key>
                <column name="rightID" />
            </key>
            <one-to-many class="pojo.RoleAuthority" />
        </set>
    </class>
</hibernate-mapping>

wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
select a.id,a.authority.id,a.name,a.description,b.id,b.name from Authority a left join Authority b on a.authority.id = b.id where a.authority.id is not null
谢谢,还是有问题

08:22:49,281 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-2) line 1:105: unexpected token: on
08:22:49,285 ERROR [dao.AuthorityDAO] (http--127.0.0.1-8080-2) find all failed: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 105 [select a.id,a.authority.id,a.name,a.description,b.id,b.name from pojo.Authority a left join Authority b on a.authority.id = b.id where a.authority.id is not null]

  • 打赏
  • 举报
回复
select a.id,a.authority.id,a.name,a.description,b.id,b.name from Authority a left join Authority b on a.authority.id = b.id where a.authority.id is not null
wuwenzhe 2013-04-18
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
where 改为on就行了
改完了还是出错

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 111 [select a.id,a.authority.id,a.name,a.description,b.id,b.name from pojo.Authority a left outer join Authority b on a.authority.id = b.id]


  • 打赏
  • 举报
回复
where 改为on就行了
wuwenzhe 2013-04-17
  • 打赏
  • 举报
回复
即使把HQL语句改写为

String queryString = "select a.id,a.authority.id,a.name,a.description,b.id,b.name from Authority a left outer join Authority b where a.authority.id = b.id";
仍然报错

18:38:03,382 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1)  Path expected for join!
18:38:03,385 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1)  Invalid path: 'b.id'
18:38:03,386 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1)  Invalid path: 'b.id'
18:38:03,387 ERROR [org.hibernate.hql.PARSER] (http--127.0.0.1-8080-1)  right-hand operand of a binary operator was null
18:38:03,388 ERROR [dao.AuthorityDAO] (http--127.0.0.1-8080-1) find all failed: org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [select a.id,a.authority.id,a.name,a.description,b.id,b.name from pojo.Authority a left outer join Authority b where a.authority.id = b.id]

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧