Results 1 to 10 of 10

Thread: JavaConfig equivalents for JPA configuration

  1. #1
    Join Date
    Jun 2008
    Posts
    4

    Default JavaConfig equivalents for JPA configuration

    I'm trying to use JavaConfig for all our Spring configuration to avoid XML programming. So far I've been quite successful until I got to two problems, both related to JPA configuration.

    The first issue is finding JavaConfig equivalent to <tx:annotationDriven/> tag. I looked at the source and there doesn't seem to be a simple bean declaration I can use in JavaConfig.

    The second issue is EntityManagerFactory bean configuration. In XML I'm using

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading .InstrumentationLoadTimeWeaver" />
    </property>
    <property name="persistenceUnitName" value="sonar" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.Oracle9iDialect" />
    </bean>
    </property>
    </bean>

    And that works fine. I've tried replacing that with the following JavaConfig (# means attribute "at" character which the forum software doesn't let me post):

    #Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
    LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
    bean.setPersistenceUnitName("sonar");
    bean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    bean.setJpaVendorAdapter(this.jpaVendorAdapter());
    return bean;
    }

    #Bean
    EntityManagerFactory entityManagerFactory() {
    return getBean(EntityManagerFactory.class);
    }

    And I get NoSuchBeanDefinitionException: No bean named "sonar" is defined. "sonar" is the name of the JPA persistence unit.

    Is there a complete example of JavaConfig JPA setup somewhere? I've searched forums and it seems that other people faced similar problems.

  2. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    Regarding tx:annotation-config:

    This is an area currently under work. Take a look at http://jira.springframework.org/browse/SJC-96, and add yourself as a watcher and/or vote for it as you see fit.

    For the time being, consider bootstrapping JavaConfig via XML with ConfigurationPostProcessor. This way you can still use tx:annotation-driven and if needs be, configure your EntityManagerFactory there, too. I realize this may not be ideal as it sounds like you're trying to get fully away from XML; hang out and we'll get there soon.

    Thanks.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3

    Default

    koollx, here is our entityManagerFactory config, give it a look:
    Code:
        @Bean
            public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(dataSource());
            entityManagerFactoryBean.setJpaDialect(new HibernateJpaDialect());
            HibernateJpaVendorAdapter jpaVendorAdapter = jpaVendorAdapter();
            entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
            Properties jpaProperties = new Properties();
            jpaProperties.setProperty("hibernate.cache.use_second_level_cache", "true");
            jpaProperties.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
            jpaProperties.setProperty("hibernate.cache.use_query_cache", "true");
            entityManagerFactoryBean.setJpaProperties(jpaProperties);
            super.getObject(entityManagerFactoryBean);
            return entityManagerFactoryBean;
        }

  4. #4
    Join Date
    Apr 2007
    Posts
    307

    Default

    Also note that I've just added a task to create a JPA/JavaConfig sample project. You can add yourself as a watcher at http://jira.springframework.org/browse/SJC-150
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  5. #5
    Join Date
    Jun 2008
    Posts
    4

    Default JavaConfig equivalents for JPA configuration

    Chris, jbaruch, thanks for your answers.

    jbaruch, I've ready your previous threads where you talked about setting JPA with JavaConfig. There were helpful. I did try defining EntityManagerFactory bean the way you've shown here in the thread, and I am currently bootstrapping JavaConfig with XML. The problem is that it seems that the beans defined in JavaConfig are not available to beans defined in XML files, or at least not available at the right time.

    For instance, I have tried defining entityManagerFactory and TransactionManager beans in JavaConfig and leaving tx:annotation-driven in XML config. I include JavaConfig into XML as follows

    Code:
        <!-- JavaConfig configuration beans and post-processor --> 
        <bean class="org.finra.hpw.config.ApplicationConfig"/>
        <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
    When I try to load Spring application context with ClassPathXmlApplicationContext I get the following exception

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot create inner bean '(inner bean)' of type [org.springframework.transaction.interceptor.TransactionInterceptor] while setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:230)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
    jbaruch, are you also using a mix of XML and JavaConfig for JPA or did you manage to get the entire config with JavaConfig?

    Chris, can I help you guys with the example or tx:annotation-driven implementation?

  6. #6

    Default

    koollx, currently my XML config only does the following:
    • Defines Components classpath scan
    • Defines Java Configuration beans
    • Defines ConfigurationPostProcessor
    • Defines tx annotations processing

    None of my business logic beans are defined in XML, so there is no need to access any JavaConfig defined beans from XML.

  7. #7
    Join Date
    Jun 2008
    Posts
    4

    Default

    That's pretty much what I have too. The only issue I see with this is that we have to use ClasspathXmlApplicationContext instead of JavaConfigApplicationContext, so I can't use a very handy type safe getBean(Class) method.

  8. #8

    Default

    Usually, you don't need to use getBean() outside of your configuration classes. What is the scenario you use it in?

  9. #9
    Join Date
    Jun 2008
    Posts
    4

    Default

    It was in a test class that used to lookup the service bean and it was later replaced with Spring test design pattern and @autowire for service instance. So right now we don't have any getBean().

  10. #10

    Default

    Yeah, that's the right way to do it :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •