0 Replies Latest reply: Aug 24, 2010 1:46 PM by 843833 RSS

    Question/Issue: JPA - ClassCastException on result from NamedQuery


      I have trouble processing the result of a named query for JPA entities. I have a JAR file containing an entity class ConfigSetting:
      @Table(name = "SETTINGS", catalog = "", uniqueConstraints = {
          @UniqueConstraint(columnNames = {"PROPERTY"})})
          @NamedQuery(name = "ConfigSetting.findAll", query = "SELECT c FROM ConfigSetting c"),
          @NamedQuery(name = "ConfigSetting.findById", query = "SELECT c FROM ConfigSetting c WHERE c.id = :id"),
          @NamedQuery(name = "ConfigSetting.findByProperty", query = "SELECT c FROM ConfigSetting c WHERE c.property = :property"),
          @NamedQuery(name = "ConfigSetting.findByValue", query = "SELECT c FROM ConfigSetting c WHERE c.value = :value")})
      public class ConfigSetting implements Serializable {
          private static final long serialVersionUID = 1L;
          @Basic(optional = false)
          @Column(name = "ID", nullable = false)
          private Integer id;
          @Basic(optional = false)
          @Column(name = "PROPERTY", nullable = false, length = 256)
          private String property;
          @Column(name = "PROPVALUE", length = 256)
          private String value;
          public ConfigSetting() {
          // Getters & Setters
      This class also has a static method getAllSettings:
      public static List<ConfigSetting> getAllSettings(EntityManager em) throws HpException {
              if (em == null) {
                  throw new HopeException("No entity manager specified!");
              try {
                  return (List<ConfigSetting>) em.createNamedQuery("ConfigSetting.findAll").getResultList();
              } catch (Exception ex) {
                  throw new HpException("Failed to get all configuration settings");
      The JAR also contains another class, having a method with the following code in the body:
      EntityManagerFactory emf = Persistence.createEntityManagerFactory(parameters[0]);
      em = emf.createEntityManager();
      List<ConfigSetting> arr = ConfigSetting.getAllSettings(em);
      for (ConfigSetting cs : arr){
        props.put(cs.getProperty().toUpperCase(), cs);
      Next to the JAR I have a WAR file (containing the persistence unit that gets loaded in the code above), containing 1 servlet calling code in the JAR that a few levels deeper in the callstack, calls the code in the snippet above. I'm using Netbeans/Glassfish v3 for debugging. When deploying this for the first time, this code works. However when running it a second time in the debugger, I get a ClassCastException on the line with message text "ConfigSetting can't be cast to ConfigSetting".
      for (ConfigSetting cs : arr)
      I've seen some posts on this and other forums that this would be due to a "classloader clash", but I can't find a solution. All help would be very appreciated.

      Thanks, Sven.