SQLGrammarException:无法执行查询:找不到列?

huangapple go评论111阅读模式
英文:

SQLGrammarException: could not execute query: Column not found?

问题

我有以下的 hibernate 查询字符串:

  1. String queryString = "select \r\n" +
  2. "cms.my_status_id as 'myStatusId',\r\n" +
  3. "cms.status_label as 'statusLabel',\r\n" +
  4. "csl.status_id as 'companyStatusLabel'\r\n" +
  5. "from "+client+".corresponding_status cms \r\n" +
  6. "join "+client+".company_status_label csl on csl.status_id = cms.my_status_id";

我的对应实体是:

  1. @Entity(name = "corresponding_status")
  2. @Table(name = "corresponding_status")
  3. public class CorrespondingStatus implements Serializable {
  4. @Id
  5. @JsonProperty
  6. @Column(name = "my_status_id")
  7. private Integer myStatusId;
  8. // varchar(255)
  9. @JsonProperty
  10. @Column(name = "status_label")
  11. private String statusLabel;
  12. @JsonProperty
  13. @Transient
  14. private String companyStatusLabel;

然而,当我运行查询时,出现了:

  1. Column 'my_status_id' not found

尽管它在数据库中确实存在。

问题出在哪里?

英文:

I have the following hibernate query string:

  1. String queryString = "select \r\n" +
  2. "cms.my_status_id as 'myStatusId',\r\n" +
  3. "cms.status_label as 'statusLabel',\r\n" +
  4. "csl.status_id as 'companyStatusLabel'\r\n" +
  5. "from "+client+".corresponding_status cms \r\n" +
  6. "join "+client+".company_status_label csl on csl.status_id = cms.my_status_id";

My Corresponding Entity is:

  1. @Entity(name = "corresponding_status")
  2. @Table(name = "corresponding_status")
  3. public class CorrespondingStatus implements Serializable {
  4. @Id
  5. @JsonProperty
  6. @Column(name = "my_status_id")
  7. private Integer myStatusId;
  8. // varchar(255)
  9. @JsonProperty
  10. @Column(name = "status_label")
  11. private String statusLabel;
  12. @JsonProperty
  13. @Transient
  14. private String companyStatusLabel;

However when I run the query I get:

  1. Column 'my_status_id' not found

even though it is definitely in the DB.

What is the issue here?

答案1

得分: 2

在HQL中,您必须使用属性而不是数据库列名。将您的HQL更改为:

  1. String queryString = "select \r\n" +
  2. "cms.myStatusId as 'myStatusId',\r\n" +
  3. "cms.statusLabel as 'statusLabel',\r\n" +
  4. "csl.status_id as 'companyStatusLabel'\r\n" +
  5. "from "+client+".corresponding_status cms \r\n" +
  6. "join "+client+".company_status_label csl with csl.status_id = cms.myStatusId";

编辑:
您可能需要相应地更改company_status_label实体。

编辑2:更改为WITH。

英文:

In HQL you must use properties instead of database column names. Change your HQL to

  1. String queryString = "select \r\n" +
  2. "cms.myStatusId as 'myStatusId',\r\n" +
  3. "cms.statusLabel as 'statusLabel',\r\n" +
  4. "csl.status_id as 'companyStatusLabel'\r\n" +
  5. "from "+client+".corresponding_status cms \r\n" +
  6. "join "+client+".company_status_label csl with csl.status_id = cms.myStatusId";

EDIT:
You probably need to change company_status_label entity accordingly

EDIT2: Changed to WITH

答案2

得分: 0

代替手动构建 JPA 查询,我建议使用Criteria API。你上面的查询将变为:

  1. Session session = HibernateUtil.getHibernateSession();
  2. CriteriaBuilder cb = session.getCriteriaBuilder();
  3. CriteriaQuery<Entity> cq = cb.createQuery(Entity.class);
  4. Root<Entity> root = cq.from(Entity.class);
  5. cq.select(root);
  6. Query<Entity> query = session.createQuery(cq);
  7. List<Entity> results = query.getResultList();
英文:

Instead of building JPA queries by hand, I would suggest the criteria API. Your query above would change from:

  1. String queryString = &quot;select \r\n&quot; +
  2. &quot;cms.my_status_id as &#39;myStatusId&#39;,\r\n&quot; +
  3. &quot;cms.status_label as &#39;statusLabel&#39;,\r\n&quot; +
  4. &quot;csl.status_id as &#39;companyStatusLabel&#39;\r\n&quot; +
  5. &quot;from &quot;+client+&quot;.corresponding_status cms \r\n&quot; +
  6. &quot;join &quot;+client+&quot;.company_status_label csl on csl.status_id = cms.my_status_id&quot;;

to something akin to:

  1. Session session = HibernateUtil.getHibernateSession();
  2. CriteriaBuilder cb = session.getCriteriaBuilder();
  3. CriteriaQuery&lt;Entity&gt; cq = cb.createQuery(Entity.class);
  4. Root&lt;Entity&gt; root = cq.from(Entity.class);
  5. cq.select(root);
  6. Query&lt;Entity&gt; query = session.createQuery(cq);
  7. List&lt;Entity&gt; results = query.getResultList();

huangapple
  • 本文由 发表于 2020年8月17日 23:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63454220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定