Sunday, 27 January 2013

Configure Entity Manager with Spring and Hibernate

First make sure you have the following dependencies in your pom
<dependency>
  <groupid>org.springframework</groupId>
  <artifactid>spring-orm</artifactId>
  <version>3.2.2.RELEASE</version>
</dependency>

<dependency>
  <groupid>org.hibernate</groupId>
  <artifactid>hibernate-core</artifactId>
  <version>4.2.1.Final</version>
</dependency>

<dependency>
  <groupid>org.hibernate</groupId>
  <artifactid>hibernate-entitymanager</artifactId>
  <version>4.2.1.Final</version>
</dependency>
Now in your spring configuration add the following
<!-- Connection details for your db -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
  <property name="username" value="sa"/>
  <property name="password" value="password"/>
</bean>

<!-- Spring provided Transaction manager -->
<bean id="transactionManager"     class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
  <property name="rollbackOnCommitFailure" value="true"/>
</bean>

<!-- Tell spring how to weave in transactions-->
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>

<!-- Entity manager factory responsible for creating managers -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  
  <!-- Adapter to use to talk to hibernate-->
  <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
  </property>
  
  <!-- specify provider props, in this case hibernate -->
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.bytecode.provider">cglib</prop>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
      <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
      <prop key="hibernate.connection.autocommit">thread</prop>
      <prop key="hibernate.cache.use_second_level_cache">false</prop>
      <prop key="hibernate.cache.use_query_cache">false</prop>
      <prop key="hibernate.transaction.flush_before_completion">true</prop>
      <prop key="hibernate.order_inserts">true</prop>
      <prop key="hibernate.order_updates">true</prop>
      <prop key="hibernate.jdbc.batch_size">50</prop>
      <prop key="hibernate.default_batch_fetch_size">50</prop>
    </props>
  </property>

  <!-- Point to domain objects -->
  <property name="packagesToScan" value="com.rizvn.domain.*" />            
</bean>
Now in your java code use:
@PersistentContext to inject in entity manager
@PersitentUnit to inject entity manager factory

For example:
  @PersistenceContext 
  EntityManager mEntityManager;

  ...
  public MyObject getMyObject(){
    return entityManager.find(MyObjectClass, 1);
  }
  ...
  
  //methods that need to commit will need transactions
  @Transactional
  public void saveMyObject(MyObject aObject){
    mEm.persist(oObject)
  }
  ..  

No comments:

Post a Comment