#

Spring Hibernate Snipplets

Few snipplets making the usage of hibernate with annotations within spring easier.

<bean name="systemProperties" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
  lazy-init="false">
  <property name="location" value="classpath:myproject.properties"/>
</bean>
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="${db.url}" />
  <property name="username" value="${db.user}" />
  <property name="password" value="${db.password}" />
  <property name="maxActive" value="${db.maxconnections}"/>
  <property name="maxIdle" value="${db.minconnections}"/>
  <property name="maxWait" value="1500"/>
  <property name="poolPreparedStatements" value="true"/>
  <property name="initialSize" value="${db.minconnections}"/>
</bean>
 
<!-- not explained here, but sets the default table type on create tables -->
<bean id="customTableTypeMySQLInnoDBDialectInjector" 
  class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="staticMethod" 
    value="de.keyboardsamurais.narcanti.common.hibernate.CustomTableTypeMySQLInnoDBDialect.setTableTypeString"/>
  <property name="arguments" value="DEFAULT CHARSET=utf8 COLLATE=utf8_bin"/>
</bean>
 
<!-- AnnotationSessionFactoryBean finds annotated beans in packages automaticaly -->	
<bean id="sessionFactory" 
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
  depends-on="customTableTypeMySQLInnoDBDialectInjector">
  <property name="dataSource" ref="dataSource"/>
  <property name="annotatedPackages" value="myproject.domain"/>
  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">
        de.keyboardsamurais.narcanti.common.hibernate.CustomTableTypeMySQLInnoDBDialect
      </prop>
      <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl.auto}</prop>
      <prop key="hibernate.order_updates">true</prop>
      <prop key="hibernate.max_fetch_depth">1</prop>
      <prop key="hibernate.jdbc.batch_size">10</prop>
      <prop key="hibernate.connection.useUnicode">true</prop>
      <prop key="hibernate.connection.characterEncoding">utf8</prop>
      <prop key="hibernate.connection.characterSetResults">utf8</prop>
      <prop key="hibernate.connection.clobCharacterEncoding">utf8</prop>
    </props>
  </property>
</bean>
 
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
 
<!-- I preferr defining this here instead of using aspects -->
<bean id="daoTransactionProxyTemplate" abstract="true" 
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionAttributeSource">
    <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
  </property>
  <property name="transactionManager" ref="transactionManager" />
</bean>
 
<!-- for all daos with setSessionFactory -->
<bean id="daoTemplate" abstract="true">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>
 
<!-- example dao -->
<bean id="myDao" parent="daoTransactionProxyTemplate">
  <property name="target">
    <bean class="myproject.dao.MyDaoImpl" parent="daoTemplate"/>
  </property>
  <property name="proxyInterfaces" value="myproject.dao.MyDao"/>
</bean>

Leave a Reply »»

Note: All comments are manually approved to avoid spam. So if your comment doesn't appear immediately, that's ok. Have patience, it can take some days until I have the time to approve my comments.