This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes  java.lang.String cannot be cast to com.action.User Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark " java.lang.String cannot be cast to com.action.User" Watch " java.lang.String cannot be cast to com.action.User" New topic
Author

java.lang.String cannot be cast to com.action.User

shekhar john
Ranch Hand

Joined: Feb 02, 2011
Posts: 38
hi to all,
i m new to struts 2 and jpa.i m getting some error "java.lang.String cannot be cast to com.action.User",
when i m retrieving the data from database my action class is;

package com.action;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.rest.DefaultHttpHeaders;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value="default")
@Namespace("/")
@ResultPath(value="/")


public class noOfUsers extends ActionSupport {

@Action(value="usersn",results={
@Result(name="create",type="tiles",location="users")
})

public static DefaultHttpHeaders create(){


EntityManagerFactory emf=Persistence.createEntityManagerFactory("tujpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
entr.begin();

Query query=em.createQuery("SELECT U.firstname from User U");
List list = query.getResultList();
System.out.println("password");
Iterator iterator = list.iterator();
while(iterator.hasNext()){
User emp = (User)iterator.next();
System.out.print("Emp Name:"+emp.getFirstname());
}
entr.commit();
em.close();



return new DefaultHttpHeaders("create");



}

}


the user is my bean .
when i m running the web application the occurs due to this line i.e User emp = (User)iterator.next();
and the error is java.lang.String cannot be cast to com.action.User
so please help me t solve the problem :

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 513
Hi,
You are not selecting all the user attributes in your Query rather you are only selecting firstname and hence the resultlist contains String objects.

regds,
amit
shekhar john
Ranch Hand

Joined: Feb 02, 2011
Posts: 38
so what can i do to retrieve only one property from database.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

You've retrieved the property. It's a String, so just treat it like a String:
shekhar john
Ranch Hand

Joined: Feb 02, 2011
Posts: 38
thanks amit punekar and Matthew Brown, for your precious advice .
i have one more problem to print it on my jsp page.
shekhar john
Ranch Hand

Joined: Feb 02, 2011
Posts: 38
hi to all ,
please guide me to show the above retrieved data on my jsp page.
 
GeeCON Prague 2014
 
subject: java.lang.String cannot be cast to com.action.User