Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Retrieve ONLY required fields/columns using JPA

843830Dec 24 2007 — edited Dec 29 2007
Hello,

I'm using JPA with Hibernate. I have a Entity 'Customer' with 30 fields which is mapped to table 'customer_table'. I want all rows but only 2 columns.
In SQL, I would have written a query:
SELECT customer_id,customer_name from customer_table
In JPA, I wrote:
Query q = em.createQuery("SELECT c.customerId, c.customerName from Customer c"); 
List<Customer> customerList = q.getResultList(); 
for(Customer customer: customerList ){ 
        System.out.println("customer Id=" + customer.getCustomerId() ); 
}
But when I try to run, I get 'java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to entity.Customer'.

Please tell how to resolve this issue.

Thank You.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 26 2008
Added on Dec 24 2007
3 comments
511 views