-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Retrieve ONLY required fields/columns using JPA
PostPosted: Mon Dec 24, 2007 12:05 am 
Newbie

Joined: Mon Oct 15, 2007 11:44 am
Posts: 7
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:
Code:
SELECT customer_id,customer_name from customer_table

In JPA, I wrote:
Code:
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.


Top
 Profile  
 
 Post subject: Attributes not the object itself
PostPosted: Wed Dec 26, 2007 9:42 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
You are trying to retrieve two attributes (customer_id,customer_name) and not a customer object itself. So you would have to do something like:


Code:
List results = q.getResultList();
Iterator it = results.iterator();
while (it.hasNext()) {
    Object[] result = (Object[]) it.next();
    System.out.println("customer Id=" + result[0]);
    System.out.println("customer Name=" + result[1]);
    // do whatever you want to do with these attributes e.g. crate a new customer object
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 11:52 am 
Newbie

Joined: Mon Oct 15, 2007 11:44 am
Posts: 7
Thank You ! (credit to you !!)
Please also help here: http://forum.hibernate.org/viewtopic.php?p=2372253


Top
 Profile  
 
 Post subject: Creste a value object
PostPosted: Mon Jan 07, 2008 10:46 am 
Newbie

Joined: Wed Aug 23, 2006 7:59 am
Posts: 7
Hi,

I don't know whether its hibernate specific or JPA, but you can create value objects using new operator;

Code:
SELECTnew CustomerListEntry(c.customerId, c.customerName) from Customer c


with CustomerListEntry having a constructor like this:

Code:
public CustomerListEntry(Long id, String name) {
...
}


This way you don't have to make the jvm create a whole customer object when all you use are the code and name attributes, lowering you memory footprint. I assume you need it for an overview page or someting?

greets,

Willem


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.