Saturday, October 18, 2014

Spring JPA/Hibernate configuration without XML

With the lovely spring feature @Configuration you can now completely drop all XML configuration. In this blog I will go through how to configure JPA/Hibernate and get rid of XML bean configuration and the persistence.xml often used with JPA. Start by instrumenting the configuration class with:

  @Configuration   
  @EnableJpaRepositories(basePackages = "com.blogspot.jpdevelopment.immutable.hibernate")   
  @EnableTransactionManagement   
  public class JpaConfig {   


@Configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container.

@EnableJpaRepositories Will scan the package of the annotated configuration class for Spring Data repositories. This means classes annotated with @Repository.

@EnableTransactionManagement Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. Typically the XML configuration looks like this:

 <tx:annotation-driven transaction-manager="transactionManager"/>  

Now it's time to declare beans. A javax.sql.DataSource bean is needed.
 @Bean  
 public DataSource dataSource() {  
      BasicDataSource dataSource = new BasicDataSource();  
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");  
      dataSource.setUrl("jdbc:mysql://localhost:3306/test");  
      dataSource.setUsername("root");  
      return dataSource;  
 }  

Next we need a JpaVendorAdapter. This serves as single configuration point for all vendor-specific properties.

 @Bean  
 public JpaVendorAdapter jpaVendorAdapter() {  
      HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();  
      hibernateJpaVendorAdapter.setShowSql(false);  
      hibernateJpaVendorAdapter.setGenerateDdl(true);  
      hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);  
   
      return hibernateJpaVendorAdapter;  
 }  

With the datasource and jpaVendorAdaptor in place we can make a LocalContainerEntityManagerFactoryBean. This will expose a EntityManagerFactory and inject it in the classes defined in the packagesToScan.
 @Bean  
 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {  
      LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();  
      entityManagerFactoryBean.setDataSource(dataSource());  
      entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());  
      entityManagerFactoryBean.setPackagesToScan("com.blogspot.jpdevelopment.immutable.hibernate");  
      return entityManagerFactoryBean;  
 }   

Finally we need a PlatformTransactionManager.

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


Now lets the it for a spin and make a repository. A simple way to get started is to use the CrudRepository interface.

 @Repository  
 public interface PersonRepository extends CrudRepository<Person, UUID> {  
 }  

And the implementation.

 public class PersonAccessRepository implements PersonRepository {  
   
      @PersistenceContext  
      private EntityManager entityManager;  
   
      @Override  
      public Person findOne(UUID id) {  
           return this.entityManager.find(Person.class, id);  
      }  
   
      @Transactional  
      @Override  
      public <S extends Person> S save(S person) {  
           this.entityManager.persist(person);  
           return person;  
      }  
 }  


Full example with dependencies and executable code can be found here.

2 comments:

  1. Casinos Near Casinos Near Casinos Near Casinos Near Me
    Las Vegas, Nevada · 김포 출장마사지 MGM National Harbor Casino & Spa · 김해 출장샵 Beau Rivage Resort & Casino · 천안 출장샵 Harrah's Hotel and Casino at Virgin 고양 출장샵 Hotels Las 속초 출장마사지 Vegas · Harrah's Casino at

    ReplyDelete