Reputation: 71
I'm new to vaadin, I'm trying to implement EclipseLink with vaadin, in my project i'm trying to get list of states from db using eclipselink and i'm getting the result set in my main file but when trying to add this items into a table using for each loop i'm getting a strange error
com.vaadin.server.ServiceException: java.lang.ClassCastException: com.example.dao.States cannot be cast to com.example.dao.States.
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = VaadintestUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final String PERSISTENCE_UNIT_NAME = "com.test.jpa";
EntityManagerFactory factory;
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select s from States s");
List<States> stateList = q.getResultList();
System.out.println("Size: " + stateList.size());
Table table = new Table("The US States");
table.addContainerProperty("Name", String.class, null);
table.addContainerProperty("Population", Integer.class, null);
for (int i = 0; i < stateList.size(); i++)
{
table.addItem(new Object[]{ stateList.get(i).getState(),stateList.get(i).getPopulation()},i);
}
table.setPageLength(table.size());
layout.addComponent(table);
}
}
<persistence-unit name="com.test.jpa" transaction-type="RESOURCE_LOCAL">
<class>com.example.dao.States</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testdb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
</properties>
</persistence-unit>
@Entity
public class States {
@Id
private Long id;
private String state;
private String population;
//Setters and getters//
Stack trace Caused By: java.lang.ClassCastException: com.example.dao.States cannot be cast to com.example.dao.States at com.example.vaadintest.VaadintestUI.init(VaadintestUI.java:69) at com.vaadin.ui.UI.doInit(UI.java:645) at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222) at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74) at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
Upvotes: 1
Views: 1249
Reputation: 2220
Try using a TypedQuery instead of Query:
TypedQuery q = em.createQuery("select s from States s", States.class); List stateList = q.getResultList();
See also the following: java.lang.ClassCastException: sametype cannot be cast to sametype Some of the people who got this error had it clear up when they restarted their server. So try a clean build, restart, and redeploy.
Upvotes: 2
Reputation: 8219
I guess the problem may be either in:
table.addContainerProperty("Population", Integer.class, null);
or
private String population;
Notice that population has different types so the query gets strings from the underlying database and then the loop tries to insert them into container property as integer values.
Replacing String population
with Integer population
in the entity seems to be the most reasonable option here.
Upvotes: 1