tags:

views:

104

answers:

1

Hi, I'm fairly new to Java Entities, and I've got a bit of a strange problem casting from a Query.getSingleResult() call. Here's a code snippet:

  LoaPoliciesConfig policy = new LoaPoliciesConfig();
  EntityManager em = getEntityManager();
  try
  {
     Query q = em.createNamedQuery("LoaPoliciesConfig.findByName");
     q.setParameter("policyName", policyName);
     Object ret = q.getSingleResult();
     policy = (LoaPoliciesConfig) ret;
  }

After working fine for ages, this has suddenly started giving me this error:

CRAAC.WebService.DataAccess.LoaPoliciesConfig cannot be cast to
CRAAC.WebService.DataAccess.LoaPoliciesConfig

At first this went away when restarting the server, but the last time it happens it seems to be here to stay.

getSingleQuery() returns an Object, and all the documentation I've found on the web so far seem to indicate this is the preferred way of doing it. So, my question is essentially am I do anything wrong in this code, or is it just the server getting confused somewhere along the way?

+3  A: 

I suspect that you have 2 LoaPoliciesConfig classes on your classpath, such that they are being loaded/accessed from two different classloaders.

In this situation, class X from one classloader is not equivalent to class X from a second classloader, and you'll get the confusion that you're seeing. Check your classpath, and the classes available in your environment, and where they're loaded from (via getClass() and getClassLoader())

Brian Agnew
Turns out this error was actually hiding more serious errors. These only showed on the first time that the error happened, from then onwards this was shown instead. I think the auto-deploy in Netbeans was causing something wierd to happen. In the end, 'undeploying' the app from the Glassfish server to see the actual error and then fix that worked.
Dan Robson