Search code examples
javahibernatejpqlrdbms

How exactly work this simple JPQL query?


I am absolutly new in Hibernate and I have the following doubt. I have this entity class and uppon my class it is decleared 2 JPQL named queries:

@NamedQueries({
        @NamedQuery(name = "kmProjectInfoStatusList", query = "select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus") ,
        @NamedQuery(name = "kmProjectInfoStatusById", query = "SELECT status  FROM KM_ProjectInfoStatus status  where lower(status.idProjectInfoStatus) = :statusId")
})

@Entity
@Table(name = "KM_PROJECT_INFO_STATUS")
public class KM_ProjectInfoStatus implements Serializable {

    @Id
    @GeneratedValue
    private Long idProjectInfoStatus;

    @Column(name = "foldertech")
    private Long foldertech;

    @Column(name = "folderproject")
    private Long folderproject;

    public Long getIdProjectInfoStatus() {
        return idProjectInfoStatus;
    }

    public void setIdProjectInfoStatus(Long idProjectInfoStatus) {
        this.idProjectInfoStatus = idProjectInfoStatus;
    }

    public Long getFoldertech() {
        return foldertech;
    }

    public void setFoldertech(Long foldertech) {
        this.foldertech = foldertech;
    }

    public Long getFolderproject() {
        return folderproject;
    }

    public void setFolderproject(Long folderproject) {
        this.folderproject = folderproject;
    }
}

So my entity class is named KM_ProjectInfoStatus and map the KM_PROJECT_INFO_STATUS database table.

My doubt is mainly related about how exactly works the JPQL named queries (I think that this is JPQL, is it correct?), consider the first query

@NamedQuery(name = "kmProjectInfoStatusList", query = "select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus")

How exactly work? I think that the selected status represent a single row of my KM_PROJECT_INFO_STATUS DB table (so it represent a specific instance of the KM_ProjectInfoStatus entity that map it)

If it is correct it means that, differently from standard SQL, in JPQL I am not selecting a subset of table fields but an entity that map an entire table row?

Tnx


Solution

  • In JPQL, we make use of classes and variable to generate query, instead of table name and column name. You will get detailed information about this from Google.

    select status from KM_ProjectInfoStatus status order by status.idProjectInfoStatus
    

    In Above JPQL query, it is retrieving all the status objects, which is object of class KM_ProjectInfoStatus which internally mapped to table KM_PROJECT_INFO_STATUS.

    So your Hibernate driver will convert this JPQL query to sql query. There are some settings through which you can print those sql queries in your console to verify the actual sql query.