1

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?

6
  • Can you show us the final query. Commented Aug 17, 2020 at 15:45
  • @jarlh how do I do so? thanks Commented Aug 17, 2020 at 15:46
  • Paste your logging output in the question Commented Aug 17, 2020 at 15:48
  • The \r\n are completely unnecessary and might be causing issues Commented Aug 17, 2020 at 15:49
  • Your CorrespondingStatus entity does not appear to have relationships to any other entities. Why are you trying to do a join here? Commented Aug 17, 2020 at 15:56

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

HQL joins don't have an ON clause.
0

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

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";

to something akin to:

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.