1

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.

PFB code snippets

Main Java File:

@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);
}

}

Persitence.xml

  <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>

States.java

@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)

anandkk
  • 71
  • 1
  • 10
  • Can we have the full stacktrace please? BTW: Why don't use use JPAContainer for this? – André Schild Dec 04 '14 at 16:37
  • hi, my project requirement is to use eclipselink, adding the stack with the actual question, the line causing exception is" table.addItem(new Object[]{ stateList.get(i).getState(),stateList.get(i).getPopulation()},i);" – anandkk Dec 05 '14 at 05:21
  • JPAContainer works great with eclispelink as JPA layer. – André Schild Dec 05 '14 at 07:08

2 Answers2

2
  1. Try using a TypedQuery instead of Query:

    TypedQuery q = em.createQuery("select s from States s", States.class); List stateList = q.getResultList();

  2. 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.

Community
  • 1
  • 1
Bampfer
  • 2,120
  • 16
  • 25
  • yes, when i restarted the server, its working fine, seems to be a conflict in class loading, will research more post the solution soon. – anandkk Dec 08 '14 at 06:15
1

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.

wypieprz
  • 7,981
  • 4
  • 43
  • 46
  • appreciate the help, but that didnt help i'm getting the same exception, added the stack in actual question. – anandkk Dec 05 '14 at 05:28
  • Right. By mixing up String and Integer I've got java.lang.IllegalArgumentException: Value is of invalid type, got java.lang.String but java.lang.Integer was expected at com.vaadin.data.util.IndexedContainer$IndexedContainerProperty.setValue(IndexedContainer.java:941) – wypieprz Dec 05 '14 at 18:08