Announcement Announcement Module
Collapse
No announcement yet.
Lazy Fetching with Spring & OpenJPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Fetching with Spring & OpenJPA

    Hello,

    I am trying to use Spring 2.5.5 in combination with OpenJPA 1.2.1. Fetching results (eagerly) and transactions work fine, but I am not able to fetch results lazily (my classes are enhanced).

    If I do it the classic way without Spring involved, it works:

    Code:
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("MyKioskDb", System.getProperties());
    		EntityManager em = factory.createEntityManager();
    		
    		List<Newspaper> result = em.createQuery("SELECT n FROM Newspaper n WHERE n.name = 'FAZ'").getResultList();
    		
    		Newspaper n = result.get(0);
    		
    		List<Kiosk> kiosks = n.getKiosks();
    		
    		em.close();
    		factory.close();
    (Kiosk is lazy fetched)

    So I think I did something wrong in my Spring configuration, which has the following settings:

    Code:
    	
    <!-- **** Settings **** -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="MyKioskDb" />
    		<property name="dataSource" ref="dataSource"/>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
    				<property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary" />
    			</bean>
    		</property>
    		<property name="jpaDialect">
                  <bean class="org.springframework.orm.jpa.vendor.OpenJpaDialect"/>
            </property>
      	</bean>
    	
    	<tx:annotation-driven transaction-manager="txManager"/>
        <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        	<property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
    	
    	<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/kioskdb"/>
    		<property name="username" value="root"/>
    		<property name="password" value=""/>
    	</bean>
    The referenced persistence unit looks that way:

    Code:
    <persistence-unit name="MyKioskDb" transaction-type="RESOURCE_LOCAL">
    		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    
    		<!-- My entity classes are here -->
    		
    		<properties>
    			<property name="openjpa.ConnectionUserName" value="root"/>
    			<property name="openjpa.ConnectionPassword" value=""/>
    			<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/kioskdb"/>
    			<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
    			<property name="openjpa.jdbc.DBDictionary" value="mysql(TableType=myisam)"/>
    			<property name="openjpa.MaxFetchDepth" value="2"/>
    		</properties>
    	</persistence-unit>
    Has anyone an idea or any hint for me?
Working...
X