Java Reference
In-Depth Information
By default, Spring will look up an exception from the
sql-error-codes.xml
file located in the
org.springframework.jdbc.support
package. However, you can override some of the mappings by
providing a file with the same name in the root of the classpath. If Spring can find your custom file, it
will look up an exception from your mapping first. However, if it does not find a suitable exception there,
Spring will look up the default mapping.
For example, suppose that you want to map your custom
DuplicateKeyException
type to error code
23505
. You have to add the binding via a
CustomSQLErrorCodesTranslation
bean, and then add this bean
to the
customTranslations
category.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
<beans>
<bean id="Derby"
class="org.springframework.jdbc.support.SQLErrorCodes">
<property name="databaseProductName">
<value>Apache Derby</value>
</property>
<property name="useSqlStateForTranslation">
<value>true</value>
</property>
<property name="customTranslations">
<list>
<ref local="myDuplicateKeyTranslation" />
</list>
</property>
</bean>
<bean id=" myDuplicateKeyTranslation"
class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
<property name="errorCodes">
<value>
23505</value>
</property>
<property name="exceptionClass">
<value>
com.apress.springenterpriserecipes.vehicle.DuplicateKeyException
</value>
</property>
</bean>
</beans>
Now if you remove the try/catch block surrounding the vehicle insert operation and insert a
duplicate vehicle, the Spring JDBC framework will throw a My
DuplicateKeyException
instead.
However, if you are not satisfied with the basic code-to-exception mapping strategy used by the
SQLErrorCodes
class, you may further implement the
SQLExceptionTranslator
interface and inject its
instance into a JDBC template via the
setExceptionTranslator()
method.
3-7. Problems with Using ORM Frameworks Directly
Suppose you are developing a course management system for a training center. The first class you create
for this system is
Course
. This class is called an
entity class
or a
persistent class
because it represents a
Search WWH ::
Custom Search