users@glassfish.java.net

Re: ClassCastException when using JPA result

From: Ryan de Laplante <ryan_at_ijws.com>
Date: Wed, 20 Aug 2008 11:17:25 -0400

I found the source of the problem. I removed all Spring persistence
related code from my DAO (@Transactional, @PersistenceContext, setter
method for entity manager) and replaced it with the following:

private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory;
   
public SystemSettingsDAOImpl() {
    entityManagerFactory = Persistence.createEntityManagerFactory("TestPU");
    entityManager = entityManagerFactory.createEntityManager();
}

I was still getting the class cast exception. Next I removed the
entity manager configuration in Spring's applicationContext.xml, and no
more class cast exception! So, this configuration is causing the problem:


<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
          <bean
class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
            <property name="databasePlatform"
value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
          </bean>
        </property>
        <property name="loadTimeWeaver">
          <bean
class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
        </property>
    </bean>

    <bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

It must be conflicting with GlassFish's internal JPA container? How
can I fix it?


Thanks,
Ryan




Ryan de Laplante wrote:
> The people in the Spring forums could only offer this advice:
>
>> You must be loading the classes twice, either they are on the
>> classpath twice, or you are (re)loading those things in different
>> servlets/listeners/applications and try to share them.
>
> Since I am really really stuck right now, I have created a sample
> project and attached it to this email. It is a Maven2 project created
> in NetBeans 6.1 for use with GlassFish V2 UR2
>
> - Web app, JSF/Facelets
> - JPA (TopLink Essentials provider)
> - Spring 2.5
> - The one and only facelet page invokes a test method on a session
> scoped managed bean (resolved by Spring). - The session scoped managed
> bean has a DAO injected into it
> - The test method in the session scoped mbean invokes a getProperty
> method on the DAO
> - The DAO uses Spring's @Transactional and @PersistenceContext to
> inject a JPA persistence unit and use transactions. It runs a basic
> query, then gets a ClassCastException when trying to use the result.
>
> It is set up to use a postgres database on localhost port 5432,
> username=postgres password=postgres You'll need to run a few
> queries to set it up:
>
> CREATE DATABASE test WITH OWNER = postgres ENCODING = 'UTF8';
>
> CREATE TABLE system_settings ("key" character varying(255) NOT NULL,
> "value" character varying(255) NOT NULL, CONSTRAINT
> system_settings_pkey PRIMARY KEY (key)) WITH (OIDS=FALSE);
>
> ALTER TABLE system_settings OWNER TO postgres;
>
> INSERT INTO system_settings("key", "value") VALUES ('systemLogLevel',
> 'DEBUG');
>
>
>
> This is an extremely bare bones basic example of using JPA with
> Spring. Can someone please point out how there are multiple
> ClassLoaders and help me move forward in my project?
>
>
>
> Thanks,
> Ryan
>
>
> Ryan de Laplante wrote:
>> Thank you for responding. Based on what I pasted in the forum post
>> (see link), what am I doing wrong?
>> I see the bean defs for entityManagerFactory:
>>
>> <property name="loadTimeWeaver">
>> <bean
>> class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
>>
>> </property>
>>
>> This is the first time I try to use JPA with Spring.
>>
>>
>> Thanks,
>> Ryan
>>
>>
>> Marina Vatkina wrote:
>>> Ryan,
>>>
>>> This is usually caused by weaving. If you have your entities weaved
>>> on the server (e.g. to support LAZY loading), you need to have them
>>> weaved either dynamically (ACC would do that) or statically (i.e.
>>> you'd need to modify/repackage the client side jar) yourself.
>>>
>>> Regards,
>>> -marina
>>>
>>> Ryan de Laplante wrote:
>>>> I loading a JPA entity from my POJO DAO that uses
>>>> @PersistenceContext and @Transactional from Spring 2.5. The result
>>>> that is returned has the same name (ex:
>>>> com.company.app.dao.SettingsEntity) but I get a class cast
>>>> exception when I try to cast from Object to the entity bean class
>>>> so that I can use it. I wrote more details here:
>>>>
>>>> http://forum.springframework.org/showthread.php?t=58964
>>>>
>>>> I'm wondering if this is a JPA issue, not Spring?
>>>>
>>>>
>>>> Thanks,
>>>> Ryan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net