That's the beauty of Spring+Hibernate - there is no special code - it's all configuration. Let me try to illustrate:
Let's say you have the following Hibernate mapping file:
Code:
<hibernate-mapping>
<class
name="org.example.Customer"
table="CUSTOMERS">
<property
name="name"
type="java.lang.String"
column="NAME"
length="1000" />
<property
name="description"
type="org.springframework.orm.hibernate.support.ClobStringType"
column="DESCRIPTION"
not-null="true" />
</class>
</hibernate-mapping>
This corresponds to the following Customer JavaBean:
Code:
public class Customer implements Serializable
{
private String name;
private String description;
public getName...
}
Let's say you have a Customer DAO:
Code:
public CustomerDAOImpl extends HibernateDaoSupport implements ICustomerDAO
{
public Customer getById(Long id)
{
return (Customer) getHibernateTemplate().get(Customer.class, id);
}
public Customer saveCustomer (Customer cust)
{
getHibernateTemplate().save(cust);
return cust;
}
}
Then in a business object somewhere you can:
Code:
public class MyBusinessObject
{
...
private ICustomerDAO customerDAO; //set with dependency injection
...
public saveNewCustomer
{
Customer c = new Customer();
c.setName("Joe");
c.setDescription("put your CLOB text in here");
customerDAO.saveCustomer(c);
}
}
Use Spring's declarative transactions to apply transactions on your business object. For example, in your applicationContext.xml:
Code:
<!-- Database Connections -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>myConnectionString</value></property>
<property name="username"><value>myUsername</value></property>
<property name="password"><value>myPassword</value></property>
<property name="defaultAutoCommit"><value>false</value></property>
<property name="maxWait"><value>3000</value></property>
<property name="maxIdle"><value>100</value></property>
<property name="maxActive"><value>10</value></property>
</bean>
<!-- NativeJdbcExtractor -->
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"
lazy-init="true"/>
<!-- LobHandler for Oracle JDBC drivers -->
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="lobHandler"><ref bean="oracleLobHandler"/></property>
<property name="mappingResources">
<list>
<value>mappingfile.hbm.xml</value>
</list>
</property>
<!-- Hibernate Properties -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.use_outer_join">true</prop>
</props>
</property>
</bean>
<!-- A transaction manager for a single JDBC connection -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<!-- your Customer Data Access Object -->
<bean id="customerDAO" class="org.example.CustomerDAOImpl">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<!-- Your business object -->
<bean id="myBusinessObjectTarget" class="org.example.MyBusinessObject">
<property name="customerDAO"><ref bean="customerDAO"/></property>
</bean>
<!-- Transactional proxy for your business object -->
<bean id="myBusinessObject" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="myBusinessObjectTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
The point is that Hibernate will user Spring's ClobStringType to save Strings in the Customer object to a CLOB column in the database with no special code. You'll need to have an active transaction (provided by myBusinessObject), and you'll need to make sure that you have an oracleLobHandler configured (+ a nativeJdbcExtractor if you're using a db connection pooling such as Apache DBCP).
The only non-standard (see jpetstore example) code in here is the stuff from my first post; specifically the nativeJdbcExtractor, oracleLobHandler, and using Spring's ClobStringType in the mapping file.