Reputation: 247
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.
Upvotes: 1
Views: 1929
Reputation: 501
I think its better for u
Long value=hibernateTemplate.find(""select count(*)from MisRecords where empReportingManagerId=?,i);
Upvotes: 0
Reputation: 4530
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();
Upvotes: 2
Reputation: 4047
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();
}
Upvotes: 3
Reputation: 1107
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.
Upvotes: 3
Reputation: 2281
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
Upvotes: 3