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

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

SQLGrammarException: could not execute query: Column not found?

问题

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

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

我的对应实体是:

@Entity(name = "corresponding_status")
@Table(name = "corresponding_status")
public class CorrespondingStatus implements Serializable {

    @Id
    @JsonProperty
    @Column(name = "my_status_id")
    private Integer myStatusId;

    // varchar(255)
    @JsonProperty
    @Column(name = "status_label")
    private String statusLabel;

    @JsonProperty
    @Transient
    private String companyStatusLabel;

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

Column 'my_status_id' not found

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

问题出在哪里?

英文:

I have the following hibernate query string:

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

My Corresponding Entity is:

@Entity(name = "corresponding_status")
@Table(name = "corresponding_status")
public class CorrespondingStatus implements Serializable {

    @Id
    @JsonProperty
    @Column(name = "my_status_id")
    private Integer myStatusId;

    // varchar(255)
    @JsonProperty
    @Column(name = "status_label")
    private String statusLabel;

    @JsonProperty
    @Transient
    private String companyStatusLabel;

However when I run the query I get:

Column 'my_status_id' not found

even though it is definitely in the DB.

What is the issue here?

答案1

得分: 2

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

String queryString = "select \r\n" +
                    "cms.myStatusId as 'myStatusId',\r\n" +
                    "cms.statusLabel as 'statusLabel',\r\n" +
                    "csl.status_id as 'companyStatusLabel'\r\n" +
                    "from "+client+".corresponding_status cms \r\n" +
                    "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

String queryString = "select \r\n" +
                "cms.myStatusId as 'myStatusId',\r\n" +
                "cms.statusLabel as 'statusLabel',\r\n" +
                "csl.status_id as 'companyStatusLabel'\r\n" +
                "from "+client+".corresponding_status cms \r\n" +
                "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。你上面的查询将变为:

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

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

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

to something akin to:

Session session = HibernateUtil.getHibernateSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery&lt;Entity&gt; cq = cb.createQuery(Entity.class);
Root&lt;Entity&gt; root = cq.from(Entity.class);
cq.select(root);
Query&lt;Entity&gt; query = session.createQuery(cq);
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:

确定