Announcement Announcement Module
Collapse
No announcement yet.
Spring 2.x JPA and RCP: ClassCastException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 2.x JPA and RCP: ClassCastException

    Hi,
    I already googled for a few days but all hints i found did not help.

    I'm developing an eclipse plugin which uses spring & jpa for database access.

    I have a dependency plugin including the following jar files:
    spring.jar, spring-aspects.jar, spring-mock.jar, toplink-api.jar, toplink-essentials.jar, commons-logging.jar, ojdbc14.jar

    In my plugin project I have an entity class called Users, an Dao interface User and the implementing class which looks like this:

    Code:
    public class UserJpaDaoImpl extends JpaDaoSupport implements UserDao
    {
        public UserJpaDaoImpl() {
            super();
        }
        public List<Users> getUsers(){
            return getJpaTemplate().find("select u from Users u");
        }
    }
    I have two applicationcontext-configuration files,
    applicationContext.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
    		default-autowire="byName">
    
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	    <property name="location" value="database.properties"/>
    	</bean>
    	
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    	    <property name="url" value="${jdbc.url}"/>
    	    <property name="username" value="${jdbc.username}"/>
    	    <property name="password" value="${jdbc.password}"/>
    	</bean>
    
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <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.oracle.OraclePlatform"/>
          </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"/>
    	    <property name="dataSource" ref="dataSource"/>
    	</bean>
    
    	<tx:annotation-driven/>
    	
    </beans>
    and daoApplicationContext.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
    		default-autowire="byName">
    
    	<bean id="userDao" class="mypackage.UserJpaDaoImpl"/>
    
    </beans>
    My persistence.xml file looks like this:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="SY_PersistenceUnit">
            <provider>SY_PersistenceProvider</provider>
            <class>mypackage.Users</class>
        </persistence-unit>
    </persistence>
    In the method void start(BundleContext context) of my plugin Activator class i call the method:

    Code:
    private void createApplicationContext() {
    		ClassLoader cl = Thread.currentThread().getContextClassLoader();
    		Thread.currentThread().setContextClassLoader(Activator.class.getClassLoader());
    		try
    		{
    			this.applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "daoApplicationContext.xml");
    			
    		}
    		finally
    		{
    			Thread.currentThread().setContextClassLoader(cl);
    		}
    	}
    Now i want to get all Users from Users and just print it out in a view of my plugin. My code looks like this:

    Code:
    ApplicationContext context = Activator.getDefault().getApplicationContext();
    UserDao userDao = (UserDao) context.getBean("userDao");
    List<Users> users = userDao.getUsers();
    
    for(Users u:users)
        System.out.println(u.getUsername());
    But now I get a ClassCastException, thrown by the line for(Users u:users) (Cannot cast mypackage.Users to mypackage.Users):

    Code:
    java.lang.ClassCastException: mypackage.Users
    	at com.lhs.ccb.sy.plugin.views.UsersView$ViewContentProvider.initialize(UsersView.java:112)
    	at com.lhs.ccb.sy.plugin.views.UsersView$ViewContentProvider.getElements(UsersView.java:66)
    	...
    I know that this is a classloader problem. Setting Thread.currentThread().setContextClassLoader(this. getClass().getClassLoader()); won't help in this case.
    It must be a problem with the loadTimeWeaver SimpleLoadTimeWeaver which seems to use a ClassLoader called SimpleInstrumentableClassLoader. But changing the LoadTimeWeaver to one of the two alternatives results in a different error thrown when trying to load my view:

    Code:
    java.lang.ClassNotFoundException: mypackage.UsersView
    ...
    I already tried to add the entries Eclipse-BuddyPolicy: registered and Eclipse-RegisterBuddy: myplugin to my plugin and my dependency plugin, but it made no difference.

    I don't know what i can do else... I appreciate every suggestion.

    Best regards
    Matthias
    Last edited by mej; Aug 9th, 2007, 09:26 AM.

  • #2
    Don't use class org.springframework.instrument.classloading.Simple LoadTimeWeaver for property loadTimeWeaver. Use specific class that is corresponding with your application server. For example, if your application server is Glassfish, use class org.springframework.instrument.classloading.glassf ish.GlassFishLoadTimeWeaver. For other application servers, refer to Spring manual

    Comment


    • #3
      thanks a lot for your reply!
      I already solved it with extending the class SimpleLoadTimeWeaver and overriding the method getInstrumentableClassLoader().

      Comment


      • #4
        I'm having the same issue

        Can you let me know what exactly you did with exending the simple load time weaver?

        Thanks,
        -L

        Comment


        • #5
          Hi!
          Unfortunately I don't have the code anymore, but I guess I just have overriden the method getInstrumentableClassLoader() where I have returned the classloader used by my plugin and updated the class of property loadTimeWeaver of the entityManagerFactory bean in the applicationContext.xml to my SimpleLoadTimeWeaver subclass.

          Comment


          • #6
            here's the solution: Create your own SimpleLoadTimeWeaver class and override the getInstrumentableClassLoader(); and update the XML descriptor to MyClassLoader

            public class MyClassLoader extends org.springframework.instrument.classloading.Simple LoadTimeWeaver {

            @Override
            public ClassLoader getInstrumentableClassLoader() {
            return super.getInstrumentableClassLoader().getParent();
            }

            }

            Comment


            • #7
              problem to call a persistence sercice in a view

              hi every one !!!
              i'm working in eclipse RCP , i've implemented the persistence with hibernate and spring together and i tested that in a main class and it works it inserts the data in my DB MySql .. i copy that code in my view as an event of the insert button !! when i run the RCP application an error appears :"Could not create the view: Plug-in "persistence" was unable to instantiate class "persistence.View". !!!

              the code that i tested succesfully in a main class :

              ApplicationContext ctx =(ApplicationContext) new FileSystemXmlApplicationContext("spring-config/ApplicationContextDao.xml");
              UserManager um = (UserManager) ctx.getBean("userManager");
              um.savePatient("hh", "ggg");


              i want to know how to implemnet my view with the persistence methods that i implemented in the layer services !!! thanks for you help

              Comment

              Working...
              X