users@glassfish.java.net

Re: ClassCastException when using JPA result (spring 2.5)

From: Ryan de Laplante <ryan_at_ijws.com>
Date: Wed, 20 Aug 2008 15:27:33 -0400

I have the JDBC30DataSource property set to true in my GlassFish
connection pool (JNDI). I can query the database with JPA when I take
Spring out of the equation. Once I add Spring's entityManagerFactory
bean then JPA stops working.


I'll restate my problem:

1) I want to use Spring 2.5.5's @Transactional and @PersistenceContext
annotations in a POJO in my Maven2 web project. I already have Spring
integrated with JSF and can confirm the DI features are working. To use
Toplink with Spring, a "load time weaver" is needed (Hibernate doesn't
need one). Using the SimpleLoadTimeWeaver I am able to use the JPA
entity's find query. When I try to cast the returned Object to my
entity class's type so that I can use it, it throws this exception:

java.lang.ClassCastException:
com.example.dao.settings.SystemSettingEntity cannot be cast to
com.example.dao.settings.SystemSettingEntity


2) If I remove Spring's entityManagerFactory bean from the
applicationContext.xml configuration file, and change my DAO to load the
entity manager itself, then I don't have any problems:

entityManagerFactory = Persistence.createEntityManagerFactory("TestPU");
entityManager = entityManagerFactory.createEntityManager();

3) If I put the code and config file back to the way it was, and change
the Load Time Weaver to the one designed for GlassFish, then GlassFish's
EJBClassLoader gets involved and throws an exception stating that it
can't find the entity class specified in my persistence.xml

Caused by: java.lang.ClassNotFoundException:
com.example.dao.SystemSettingEntity
        at
com.sun.enterprise.loader.EJBClassLoader.findClassData(EJBClassLoader.java:718)
...

4) If I remove any mention of entity classes from persistence.xml and
configure it to automatically find annotated classes, then it never
finds or initializes my entity.

If you have time, could you please create a sample web project that
demonstrates Spring 2.5.5 being used with JPA on GlassFish? I created
a sample app earlier today and attached it to an email in this thread if
anyone wants to try it.


Thanks,
Ryan


Manfred Riem wrote:
> I see you are running PostgreSQL? Make sure the case of table name
> matches the case you specified in the database. Also, make sure that
> your driver is specifying it is a JDBC 3.0 connection.
>
> To verify I would recommend trying it against a Mysql database, if
> that one works you know it has to do with the driver somehow.
>
> Manfred
>