2012-08-07 79 views
3

我有兩個疑問運行:爲什麼我的「左連接」正在拋出「意外令牌:上」錯誤?

2012-08-07 11:24:02,561 ERROR [org.hibernate.hql.PARSER] (http-0.0.0.0-8080-12) line 1:105: unexpected token: ON 
2012-08-07 11:24:02,686 ERROR [org.jboss.aspects.tx.TxPolicy] (http-0.0.0.0-8080-12) javax.ejb.EJBTransactionRolledbackException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1, column 105 
[SELECT count(o) from ejb.entity.News o LEFT JOIN NewsNewsDistributionServiceLink p ON o.id = p.newsId WHERE o.statusId = ?1 AND p.newsDistServiceCode is ?2] 

2012-08-07 11:26:15,605 ERROR [org.hibernate.hql.PARSER] (http-0.0.0.0-8080-12) line 1:105: unexpected token: ON 
2012-08-07 11:26:15,609 ERROR [org.jboss.aspects.tx.TxPolicy] (http-0.0.0.0-8080-12) javax.ejb.EJBTransactionRolledbackException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1, column 105 
[SELECT count(o) from entity.News o LEFT JOIN NewsNewsDistributionServiceLink p ON o.id = p.newsId WHERE o.companyId = ?1 AND o.statusId = ?2 AND p.newsDistServiceCode is ?3] 

我跑了SQuirreL SQL客戶端執行以下操作:

select * from news n left join news_news_distribution_service_link nl 
on n.id=nl.news_id 
where news_distribution_service_code is null 

這將返回結果,但是當我翻譯這對我的Java Servlet,它拋出錯誤。

新聞臺:

id -PK 
company_id 
user_id 
title 
type_id 
status_id 
location 
is_immediate 
contents 
created_date 
last_modified_date 
release_date 
delete_date 
corrected_news_id 
can_distribute 
was_distributed 

NewsNewsDistLink表:

id -PK 
news_id - this is equal to the id in News 
news_distribution_service_code 

代碼:

String _query = "SELECT COUNT(o) FROM News o " + (filter.tierGroup != null ? TIER_GROUP_JOIN : "") + "WHERE "; 
      WhereClauseBuilder builder = getWhereClause(filter); 
      _query+= builder.where.toString(); 

      Query query = em.createQuery(_query); 
      query.setHint("org.hibernate.cacheable", true); 
      builder.bind(query); 

的whereClauseBuilder()基本上 「其中」 後加參數,可以在bind()基本上將參數與##及其值綁定。最後,p.newsDistServiceCode在大多數情況下很可能爲空(> 95%)。

+0

發佈您的Java和Hibernate代碼 – 2012-08-07 16:41:43

+0

@Michael添加了代碼 – iCodeLikeImDrunk 2012-08-07 16:51:30

+0

而不是COUNT(o)您嘗試了COUNT(*)嗎? – contactmatt 2012-08-07 18:39:33

回答

0

因爲查詢不是SQL查詢,而是HQL查詢。 SQL和HQL是兩種不同的語言。 SQL在表和列上工作,而HQL在Hibernate實體和關聯上工作。

查看Hibernate documentation

相關問題