1

Test.java

public static void main(String[] args) {
    System.out.println("Enter reporting manager empid");
    Scanner sc = new Scanner(System.in);
    Integer in = sc.nextInt();
Session session=new  AnnotationConfiguration().configure().buildSessionFactory().openSession();
 Query query=session.createQuery("select count(*)from MisRecords where empReportingManagerId=:i");
 query.setParameter("i", in);
    List<MisRecords> list=query.list();{
    for(MisRecords employee:list){
        //  System.out.println(employee.getFirstName());
        //  System.out.println(employee.getLastName());
         // System.out.println(employee.getEmpId());
          System.out.println(employee.getFirstName()+" "+ employee.getEmpId()+ " "+employee.getEmpReportingManagerId());

     }
MisRecords.java

@Entity
@Table(name="dat_emprecords")
public class MisRecords {
@Id
@GeneratedValue
@Column(name="pk_EmpRec_Idx")
    int id;

    @Column(name="EmpRec_EmpFName")
    String firstName;
    @Column(name="EmpRec_EmpLName")
    String lastName;
    @Column(name="fk_EmpRec_EmpID")
    int empId;
    @Column(name="fk_emprec_empreportingmgrid")
    int empReportingManagerId;

//output Enter reporting manager empid 1 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to com.saurabh.MisRecords at com.saurabh.Test.main(Test.java:17)

When i am trying to run i am getting this classCastException ,Don't know why? please help. thanks in advance.

Amit Das
  • 1,077
  • 5
  • 17
  • 44
saurabh
  • 237
  • 6
  • 20

5 Answers5

3

Your query is returning the count of results which is long. Please do :

select * from MisRecords where empReportingManagerId=:i

insetad of

select count(*)from MisRecords where empReportingManagerId=:i to get the results from the query.

If you want the count, you can do the following:

Query query=session.createQuery("select count(*)from MisRecords where empReportingManagerId=:i");
query.setParameter("i", in);
Iterator itr =query.iterator();
int i = 0;
if(itr.hasNext()){
    i = itr.next().intValue();
}
shruti1810
  • 3,920
  • 2
  • 16
  • 28
  • i want to see how many records are there instead to select all records – saurabh Jun 02 '15 at 04:42
  • Then you should not be doing the following operation on that : `List list=query.list();` As you know the result of the query is Long and not MisRecords. – shruti1810 Jun 02 '15 at 04:44
  • 1
    You might do a query.iterate() and take the first result in the Long variable. – shruti1810 Jun 02 '15 at 04:46
  • int a=( (Long) session.createQuery("select count(*) from MisRecords where empReportingManagerId=1").iterate().next() ).intValue(); ,i found this solution ,but here i am hard coding the id,how i can provide value from scanner – saurabh Jun 02 '15 at 04:59
  • its correct but this answer Query query=session.createQuery("select count(*)from MisRecords where empReportingManagerId=:i"); Long recordCount = (Long) query.uniqueResult(); is more efficient ,i will upvote your answer but. – saurabh Jun 02 '15 at 08:47
3

You are getting a long from your query because you are using select count, if you wanna retrieve all the entities use from entity or select e from entity e

Kennedy Oliveira
  • 2,141
  • 2
  • 20
  • 25
3

You have used count(*) in your query. Thats why it will give result as a long value. if you want to get only count then you can execute the query and after that do query.iterate() and take the first result in long variable.

Amit Das
  • 1,077
  • 5
  • 17
  • 44
  • int a=( (Long) session.createQuery("select count(*) from MisRecords where empReportingManagerId=1").iterate().next() ).intValue(); ,i found this solution ,but here i am hard coding the id,how i can provide value from scanner – saurabh Jun 02 '15 at 05:00
  • You can use direct variable name in the query. like this - int a=( (Long) session.createQuery("select count(*) from MisRecords where empReportingManagerId="+VARIABLE_NAME).iterate().next() ).intValue(); – Amit Das Jun 02 '15 at 05:45
2

As you mentioned in the comment, I see that you are trying to see record count,

i want to see how many records are there instead to select all records

For that use below code snippet,

Query query=session.createQuery("select count(*)from MisRecords where empReportingManagerId=:i");
Long recordCount = (Long) query.uniqueResult();
Gaurang Patel
  • 4,310
  • 6
  • 27
  • 32
0

I think its better for u

Long value=hibernateTemplate.find(""select count(*)from MisRecords where empReportingManagerId=?,i);
Chandu D
  • 505
  • 3
  • 17