Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7605155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:05:38+00:00 2026-05-31T00:05:38+00:00

I am getting an error with Hibernate (I am not too familiar the API

  • 0

I am getting an error with Hibernate (I am not too familiar the API yet). Here is my code below:

public void retrieveToDB(String data){
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();

    Query q = session.createQuery("Select empName from Employee where empName = :emp");
    q.setParameter("emp", data);
    List result = q.list();

    Iterator iter = result.iterator();

    while(iter.hasNext()){
        Employee emp = (Employee) iter.next();
        String e = emp.getEmpName();
        System.out.println("THIS IS " +e);
    }



}     

I wanted to retrieve an employee name from employee table and put into a test string to be sent to the client side. But my problem is I am getting this error which I am very unfamiliar with:

  Exception in thread "Thread-0" java.lang.ClassCastException: java.lang.String cannot be cast to com.datadistributor.main.Employee

I tried to find some tutorials regarding lists handling in Hibernate but I keep finding the same (unhelpful) stuff. So far nothing I’ve tried has worked out, I hope someone can help me out.

ok i updated my code and i get again new error…is there anything wrong? i hope can get some help here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:05:40+00:00Added an answer on May 31, 2026 at 12:05 am

    I think what you’re looking to do is something closer to this:

    public void retrieveToDB(String data) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        // I don't think you need to start a transaction just to do a SELECT statement
        //session.beginTransaction();
    
        Query q = session.createQuery("SELECT e from Employee e where e.empName = :empname");
        q.setParameter("empname", data);
        List results = (List) q.list();
    
        String test = null;
        Iterator iter = results.iterator();
    
        while (iter.hasNext()) {
            Employee emp = (Employee) iter.next();
            test = emp.getEmpName();
        }
    
        sendMsg(test);
    }
    

    There’s no need to store your query’s results into an enclosing List (or ArrayList) if you’re just planning to unwrap it to get at the underlying Employee object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this Hibernate error: org.hibernate.MappingException: Could not determine type for: a.b.c.Results$BusinessDate, for columns:
I have some inherited code that uses hibernate. I'm getting the following error: Caused
Error: org.hibernate.LazyInitializationException SEVERE: could not initialize proxy - the owning Session was closed Code:
I am getting the following error org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException:
I am getting error trying to run my asp code for executing stored proc.
I'm getting error at User.findById(user_id) not frequently but some times, I don't know why.
I am getting the following error. Not getting clue why is it happening. Also
I'm getting syntax error for Hibernate 3.3.2 generated queries containing double-quotes: create table tbl_version
I am configuring Struts2 and Hibernate frameworks, and I am getting an error from
I am getting this error when i am calling a Hibernate Criteria with hibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.