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.

1
  • check out this link..it might help you with your issue.. Commented Jun 2, 2015 at 5:04

5 Answers 5

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

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

Comments

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.

2 Comments

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

5 Comments

i want to see how many records are there instead to select all records
Then you should not be doing the following operation on that : List<MisRecords> list=query.list(); As you know the result of the query is Long and not MisRecords.
You might do a query.iterate() and take the first result in the Long variable.
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
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.
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();

Comments

0

I think its better for u

Long value=hibernateTemplate.find(""select count(*)from MisRecords where empReportingManagerId=?,i);

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.