0

My application is a Spring MVC and hibernate based one. In the below hql statement I'm mapping the resultset to the custom class named 'Result'. Here is my HQL statement,

        String hql=
                "select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name)"+
                " from com.gavs.beans.College c" + 
                " join com.gavs.beans.Department d on d.institutes.college_id = c.college_id";

        org.hibernate.query.Query q = session.createQuery(hql);

        List<Result> r1= q.list();


       return r1;

I get these following exceptions in the hql statement

Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:34: unexpected token: distinct
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:34: unexpected token: distinct
line 1:34: unexpected token: distinct
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:43: unexpected token: d
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:43: unexpected token: d
line 1:43: unexpected token: d
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:56: unexpected token: as
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:56: unexpected token: as
line 1:56: unexpected token: as
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:92: expecting EOF, found ')'
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:92: expecting EOF, found ')'
line 1:92: expecting EOF, found ')'
.
.
.
Jun 09, 2020 3:33:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [studentDetails] in context with path [/HibernateWithQuery] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 34 [select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name) from com.gavs.beans.College c join com.gavs.beans.Department d on d.institutes.college_id = c.college_id]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 34 [select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name) from com.gavs.beans.College c join com.gavs.beans.Department d on d.institutes.college_id = c.college_id]

Here is the Student Entity (Student.java)

package com.gavs.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="STUDENT_TABLE")
public class Student{

    @Id
    @Column(name="Roll_No")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int roll_no;

    @Column(name="Name")
    private String name;

    @Column(name="Age")
    private int age;

    @Column(name="Address")
    private String address;

    @ManyToOne
    @JoinColumn(name="Dept_Id")
    private Department departments;



    public Student() {
        super();
    }

        public int getRoll_no() {
        return roll_no;
    }

    public void setRoll_no(int roll_no) {
        this.roll_no = roll_no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    public Department getDepartments() {
        return departments;
    }

    public void setDepartments(Department departments) {
        this.departments = departments;
    }


}

This is the Department Entity(Department.java)

package com.gavs.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="DEPARTMENT_TABLE")
public class Department {

    @Id
    @Column(name="Dept_Id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int dept_id;

    @Column(name="Dept_Name")
    private String dept_name;

    @Column(name="Dept_Code")
    private int dept_code;

    @ManyToOne
    @JoinColumn(name="College_Id")
    private College institutes;


    public int getDept_id() {
        return dept_id;
    }


    public Department() {
        super();
    }


    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }

    public String getDept_name() {
        return dept_name;
    }
    public void setDept_name(String dept_name) {
        this.dept_name = dept_name;
    }

    public int getDept_code() {
        return dept_code;
    }
    public void setDept_code(int dept_code) {
        this.dept_code = dept_code;
    }


     public College getColleges() {
        return institutes;
    }


    public void setColleges(College institutes) {
        this.institutes = institutes;
    }


}

This is the College Entity(College.java)

package com.gavs.beans;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="COLLEGE_TABLE")
public class College {

@Id
@Column(name="College_Id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int college_id;

@Column(name="College_Name")
private String college_name;

@Column(name="College_Address")
private String college_address;

@OneToMany(mappedBy="institutes",cascade=CascadeType.ALL)
private List<Department> departments;

public College() {

}

public int getCollege_id() {
    return college_id;
}
public void setCollege_id(int college_id) {
    this.college_id = college_id;
}

public String getCollege_name() {
    return college_name;
}
public void setCollege_name(String college_name) {
    this.college_name = college_name;
}

public String getCollege_address() {
    return college_address;
}
public void setCollege_address(String college_address) {
    this.college_address = college_address;
}
public List<Department> getDepartments() {
    return departments;
}

public void setDepartments(List<Department> departments) {
    this.departments = departments;
}

}

2
  • Please provide the code of the used classes (Result, Collage, Department, Institute) Commented Jun 10, 2020 at 9:58
  • Thanks for your reply. I have posted all my classses. But when I used the distinct keyword outside the constructor it worked. Commented Jun 10, 2020 at 10:04

1 Answer 1

2

Try to use Interface projection with JPA repository

public interface Result{
    String getDepartmentName();
    String getCollegeName();
}

And in your Jpa repository:

@Query("select distinct d.dept_name as departmentName, d.institutes.college_name as collegeName from Department d")
List<Result> findDistinctDepartmentAndCollegeName();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer Nosairat. The findDistinctDepartmentAndCollegeName() is an inbuilt method ?? Also Should I create an implementation class for the interface Result?
Also please let me know how to store the Result list in a List variable so that I can pass it to JSP page and iterate the list and print the values.
No, the method is not built-i, and you can place it in any spring data repository ( see baeldung.com/the-persistence-layer-with-spring-data-jpa ). No need to create an implementation, the framework will do that for you.
Okay Nosairat. Thank you very much for your help.

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.