JPA Spring Java Config

Simple code snippet which demonstrates how to configure JPA with Spring using java configuration.

@Configuration
@EnableJpaRepositories(basePackages = { "com.satesh.springjsfjpa.repo" })
@EnableTransactionManagement
public class RepositoryConfig {

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.build();
}

@Bean
public PlatformTransactionManager transactionManager() {
EntityManagerFactory factory = entityManagerFactory().getObject();
                return new JpaTransactionManager( factory );
}

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(Boolean.TRUE);
        vendorAdapter.setShowSql(Boolean.TRUE);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.satesh.springjsfjpa.entity");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
        factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        //return factory.getObject();
        //Changed as per Josh's suggestion
       return factory;
    }

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()
{
return new HibernateExceptionTranslator();
}
}

And here is web application java configuration using annotation based..

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.satesh.springjsfjpa" })
@Import({PropertyPlaceHolderConfig.class, RepositoryConfig.class})
public class ApplicationConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver internalResourceViewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

@Bean
public ResourceBundleMessageSource resourceBundleMessageSource()
{
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}

@Override   
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
registry.addResourceHandler("/resources/css/**").addResourceLocations("/resources/css/").setCachePeriod(31556926);
}

@Override   
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
{       
configurer.enable();   
}
}

Who's new

  • Arun Verma
  • PHANIKUMAR
  • Ashish_Tripathi
  • rajeswari
  • harishkmt

Who's online

There are currently 0 users online.

Recent Posts

Its been long time since I wrote article.

Simple code snippet which demonstrates how to configure JPA with Spring

Quick and simple, if you want to create a simple spring application whi

Go to top