Announcement Announcement Module
Collapse
No announcement yet.
Spring 2.5 + JPA with EclipseLink / Toplink table generation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 2.5 + JPA with EclipseLink / Toplink table generation

    Hi,

    I'm new to Spring and trying to evaluate a JPA-based data access setup with not much success. The goal is to create a simple Spring 2.5.x configuration with JPA using Eclipselink as the implementation. The setup should be deployable to Tomcat (or any web container, Glassfish and Tomcat are the to servers preferred), and I need Spring/Eclipselink to generate the tables (and/or the SQL scripts) for the entities (as a regular EJB3 deployment would).

    The result is that the app is deployable but on Tomcat, nothing happens to the database. On GF, the default JDBC pool from JNDI (named jdbc/__default) is used to find the database and create the tables, so possibly something's wrong with the classloading and weaving I guess because it's a GF default behavior.

    I know I'm screwing up something, I just have no clue what

    What I did is the following:

    created a WEB-INF/applicationContext.xml with the followin contents:
    Code:
    <beans...>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql:localhost:3306/springtest" />
            <property name="username" value="springtest" />
            <property name="password" value="pass" />
        </bean>
    
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
            <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
            </property>
            <property name="persistenceProvider">
                <bean class="org.eclipse.persistence.jpa.PersistenceProvider" />
            </property>
        </bean>
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
            <property name="persistenceXmlLocation" value="META-INF/persistence.xml" />
            <property name="persistenceUnitName" value="myPU" />
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <tx:annotation-driven mode="aspectj" />
        <tx:jta-transaction-manager />
        <context:annotation-config />
        <context:load-time-weaver aspectj-weaving="on" />
        <context:component-scan base-package="org.springtest" />
    </beans>

    modified META-INF/context.xml for Tomcat:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context antiJARLocking="true" path="/testapp">
        <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" />
    </Context>
    and also placed spring-tomcat-weaver.jar in tomcat's lib/ folder, and also added -Djavaagent="c:/program files/tomcat/lib/spring-agent.jar" to the Tomcat JAVA_OPTs (I hope Tomcat finds it )

    Let's assume that the app correctly deploys to the /testapp context (it does).

    created one TestEntity:

    Code:
    package org.springtest.domain;
    
    import ...
    
    &#064;Entity
    public class TestEntity implements Serializable { ... }
    created one META-INF/persistence.xml in the classpath (assembled to WEB-INF/classes/META-INF/persistence.xml

    Code:
    <persistence ...>
      <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
        <properties>
          <property name="eclipselink.ddl-generation" value="create-tables"/>
        </properties>
      </persistence-unit>
    </persistence>
    And now I'm waiting for the magic to happen. Any ideas, hints, where to start debugging?

    Thanks a lot, folks!

  • #2
    Investigating more, I've found out that using toplink-1.0, it generates the entities, using toplink-2.x and eclipselink, it does not. Does anybody know of JPA-related a Spring configuration change, or is it clearly a toplink bug/feature?

    Comment


    • #3
      I'm using eclipselink 1.x without problems.

      Did you list your entities in the presistence.xml, i.e.
      Code:
      	<persistence-unit name="my-unit" transaction-type="RESOURCE_LOCAL">	
      		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      		<class>org.mypackage.MyEntity</class>
      	</persistence-unit>
      </persistence>
      If you are not sure about the load-time weaving working, replace loadTimeWeaver with

      Code:
      		<property name="jpaPropertyMap">
              	<map>
              		<entry key="toplink.weaving" value="static"/>
              	</map>
              </property>
      as a test.

      When it comes to debugging etc, I wouldn't develop an application not testable from the IDE, i.e unit/integration tests with debug possibilities from the IDE.

      Cheers

      G

      Comment


      • #4
        Hi,

        Originally posted by Goran View Post
        I'm using eclipselink 1.x without problems.

        Did you list your entities in the presistence.xml, i.e.
        Code:
        	<persistence-unit name="my-unit" transaction-type="RESOURCE_LOCAL">	
        		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        		<class>org.mypackage.MyEntity</class>
        	</persistence-unit>
        </persistence>
        Nope. With Toplink 1.0 or the most recent Hibernate package it wasn't needed (I guess with the help of <context:component-scan base-package="some.thing" /> and the config just works. I assumed that it's not necessary but I'll give it a shot, thanks.

        Originally posted by Goran View Post
        If you are not sure about the load-time weaving working, replace loadTimeWeaver with

        Code:
        		<property name="jpaPropertyMap">
                	<map>
                		<entry key="toplink.weaving" value="static"/>
                	</map>
                </property>
        I'll try this, too. Anyways, the setup works with the most recent Hibernate version (3.3.x) so one part of me is happy just this toplink error is strange.

        Tamas

        Comment

        Working...
        X