Results 1 to 5 of 5

Thread: 3.0.0M3: How to configure JPA EntityManagerFactory ?

  1. #1

    Default 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. #2

    Default

    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;
    }
    }

  3. #3

    Question 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?

  4. #4

    Default

    I'm still using the code above.

  5. #5

    Lightbulb 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

Posting Permissions

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