For me, this didn't work. The Set<MyEnumType> eventually contained String
objects.
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 {
CLEANING;
}
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
CUSTOMER_SERVICES -->
<select id="getCustomerById" parameterClass="int"
resultMap="customerResultMap">
{call customer_getById(#value#)}
</select>
<!-- 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="customer.ServicesForCustomerResultMap"/>
</resultMap>
<resultMap id="ServicesForCustomerResultMap" class="Service">
<!-- CUSTOMER_SERVICES contained two main columns: customer_id and
service_name -->
<result property="value" column="service_name"/>
</resultMap>
</sqlMap>
My SQLmapConfig.xml:
<sqlMapConfig>
<typeHandler javaType="com.acme.Service"
callback="com.acme.ServiceTypeHandler" />
<sqlMap resource="[link to sqlMap file]"/>
</sqlMapConfig>
My Type Handler Class
public class ServiceTypeHandler implements TypeHandlerCallback {
@Override
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;
}
@Override
public void setParameter(ParameterSetter setter, Object parameter)
throws
SQLException {
if (parameter == null) {
setter.setNull(Types.VARCHAR);
} else {
Service service = (Service) parameter;
setter.setString(service.toString());
}
}
@Override
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: [email protected]
For additional commands, e-mail: [email protected]