Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
3.0.0M3: How to configure JPA EntityManagerFactory ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 3.0.0M3: How to configure JPA EntityManagerFactory ?

    So far I figured out the following config methods. However, I've no idea how to obtain an EntityManagerFactory using the annotations provided in 3.0.0M3. Any hint is appreciated!


    @Bean(destroyMethod="close")
    public DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(...);
    ds.setUrl(...);
    ds.setUsername(...);
    ds.setPassword(...);
    ds.setMaxActive(...);
    ds.setMinIdle(...);
    return ds;
    }
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
    return new HibernateJpaVendorAdapter();
    }
    @Bean
    public LoadTimeWeaver loadTimeWeaver() {
    return new InstrumentationLoadTimeWeaver();
    }
    @Bean
    public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();
    emfBean.setDataSource(dataSource());
    emfBean.setJpaVendorAdapter(jpaVendorAdapter());
    emfBean.setLoadTimeWeaver(loadTimeWeaver());
    EntityManagerFactory emf = ??? // How do I proceed ???
    return emf;
    }
    @Bean
    public PlatformTransactionManager transactionManager() {
    return new JpaTransactionManager(entityManagerFactory());
    }

  • #2
    With the help of org.springframework.config.java.support.Configurat ionSupport I implemented the following (and it works):


    @Configuration
    public class SpringConfig implements BeanFactoryAware, ApplicationContextAware {
    ...
    private AutowireCapableBeanFactory autowireCapableBeanFactory;
    private ApplicationContext applicationContext;
    ...
    @Bean
    public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();
    emfBean.setDataSource(dataSource());
    emfBean.setJpaVendorAdapter(jpaVendorAdapter());
    emfBean.setLoadTimeWeaver(loadTimeWeaver());

    return getConfigured(emfBean).getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
    return new JpaTransactionManager(entityManagerFactory());
    }


    @Override // BeanFactoryAware
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof AutowireCapableBeanFactory) {
    autowireCapableBeanFactory = (AutowireCapableBeanFactory) beanFactory;
    }
    }


    @Override // ApplicationContextAware
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    }

    private <T> T getConfigured(T object) {
    if (autowireCapableBeanFactory == null)
    throw new IllegalStateException("LocalContainerEntityManager FactoryBean not configurable");

    @SuppressWarnings("unchecked")
    T configuredObject = (T) autowireCapableBeanFactory.initializeBean(object, null);

    if (applicationContext != null) {
    if (configuredObject instanceof ResourceLoaderAware) {
    ((ResourceLoaderAware) configuredObject).setResourceLoader(applicationCon text);
    }
    if (configuredObject instanceof ApplicationContextAware) {
    ((ApplicationContextAware) configuredObject).setApplicationContext(applicatio nContext);
    }
    }

    return configuredObject;
    }
    }

    Comment


    • #3
      Really - this is the solution?

      I have a similar issue but use (unsuccessfully) the createNativeEntityManagerFactory() method. It seems that this should work since it is the only method to get at the entitymanager factory.

      Was this how you left it and is this what Spring expects?

      Comment


      • #4
        I'm still using the code above.

        Comment


        • #5
          Possible solution

          I think if you call afterPropertiesSet() you can call use the exposed entitymanager. I need to check on the 3.0.1 snapshot (JPA bug is fixed there) when it is I will update here so you know.

          I am also going to log a jira to update docs so others will know this

          Comment

          Working...
          X