Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
shekhar john
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what can i do to retrieve only one property from database.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've retrieved the property. It's a String, so just treat it like a String:
 
shekhar john
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all ,
please guide me to show the above retrieved data on my jsp page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic