ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From AsafM <asaf.mes...@gmail.com>
Subject Re: Mapping of a Set with enums
Date Mon, 18 Jan 2010 15:17:28 GMT

For me, this didn't work. The Set<MyEnumType> eventually contained String

Here's how I solved it:

I have a class called Customer which contains a a set of Service. The
customer is saved in CUSTOMERS and the services in CUSTOMER_SERVICES.

The Enum class
public enum Service {

My bean using the enum class as a set:
public class Customer {

    private Set<Service> services;

My SQLmap.xml file:

<sqlMap namespace="customer">

<typeAlias alias="Service" type="com.acme.Service"/>

<!-- The following select returns a join between CUSTOMERS and
<select id="getCustomerById" parameterClass="int"
     {call customer_getById(#value#)}

<!-- the groupBy element is a key factor in the solution here -->
<resultMap id="customerResultMap" class="com.acme.Customer" groupBy="id">
   <result property="id" column="id"/>
   <result property="name" column="name"/>
    <!-- The typeHandler for the Service enum is defined in the
sqlMapConfig.xml file.
           It's important to include the namespace (customer) as the prefix
for resultMap!
    <result property="services"

<resultMap id="ServicesForCustomerResultMap" class="Service">
  <!-- CUSTOMER_SERVICES contained two main columns: customer_id and
service_name -->
  <result property="value" column="service_name"/>  

My SQLmapConfig.xml:
    <typeHandler javaType="com.acme.Service" 
                 callback="com.acme.ServiceTypeHandler" />
    <sqlMap resource="[link to sqlMap file]"/>

My Type Handler Class 
public class ServiceTypeHandler implements TypeHandlerCallback {

	public Object getResult(ResultGetter getter) throws SQLException {
        String serviceString = getter.getString();

        // This has to be called *after* getString()!
        if (getter.wasNull()) {
            return null;
        Service service = null;
        try {
            service = Service.valueOf(serviceString);
        } catch (Exception e) {
            throw new RuntimeException("Database value for Service contain
an " +
                    "unrecognized Service: " + serviceString, e);
        return service;

	public void setParameter(ParameterSetter setter, Object parameter) throws
SQLException {
        if (parameter == null) {
         } else {
             Service service = (Service) parameter;

	public Object valueOf(String valueAsString) {
	    // I'm not sure if we actually need it.
        return valueAsString;


View this message in context: http://old.nabble.com/Mapping-of-a-Set-with-enums-tp23602196p27211940.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.

To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org

View raw message