You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Mikael Andersson <ma...@gmail.com> on 2009/05/18 19:40:37 UTC

Mapping of a Set with enums

Hi

I'm having problems mapping to a property which is a Set of java5 enums.

I have a class CounterpartyHedgeVO which has the property creditEvents which
is a Set<CreditEventEnum>.

Not sure what the problem is but I'm guessing that iBATIS tries to treat the
Enum like a POJO and fails to create it since there isn't a public
constructor. Or?

Any help would be really appreciated.

My current sqlmap (omitted the typeAlias definitions):

<resultMap class="CounterpartyHedgeVO" id="CounterpartyHedgeVOResult"
groupBy="refNo">
    <result column="POS_RefNoADB" jdbcType="VARCHAR" property="refNo"/>
   <result column="PRT_Code" jdbcType="VARCHAR" property="prtCode"/>
      <result column="RBE_CodeCpty" jdbcType="VARCHAR"
property="counterpartyCode"/>
      <result column="POS_Tradebook"  jdbcType="VARCHAR"
property="tradeBook"/>

      <result resultMap="CDS_HEDGE.CreditEventEnumResult"
property="creditEvents"/>
 </resultMap>

 <resultMap class="CreditEventEnum" id="CreditEventEnumResult">
  <result column="CRE_EventType" property="value" jdbcType="VARCHAR"
typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
 </resultMap>

Stack trace:
--- The error occurred in ibatis/sqlmap-map-cds-hedge.xml.
--- The error occurred while applying a result map.
--- Check the CDS_HEDGE.CreditEventEnumResult.
--- The error occured while instantiating the result object
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not
instantiate result class.  Cause: java.lang.InstantiationException:
hedge.interfaces.CreditEventEnum
 at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
 at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
 at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:615)
 at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:589)
 at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
 at
org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
 at
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
 ... 40 more
Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not
instantiate result class.  Cause: java.lang.InstantiationException:
hedge.interfaces.CreditEventEnum
 at
com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
 at
com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
 at
com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue(BasicResultMap.java:449)
 at
com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap(BasicResultMap.java:396)
 at
com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:382)
 at
com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
 at
com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
 at
com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
 at
com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQueryProcedure(SqlExecutor.java:283)
 at
com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.sqlExecuteQuery(ProcedureStatement.java:34)
 at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
 ... 46 more
Caused by: java.lang.InstantiationException:
hedge.interfaces.CreditEventEnum
 at java.lang.Class.newInstance0(Class.java:340)
 at java.lang.Class.newInstance(Class.java:308)
 at com.ibatis.common.resources.Resources.instantiate(Resources.java:294)
 at
com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:127)
 at
com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:95)
 at
com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
 ... 56 more

Cheers,
 Micke

Re: Mapping of a Set with enums

Posted by Kai Grabfelder <no...@kaigrabfelder.de>.
Hi Mikael,

could you try to map creditEvents to a Set of Strings instead and see if this works?

Regards

Ka

--- Original Nachricht ---
Absender: Mikael Andersson
Datum: 18.05.2009 23:52
> Hi
> 
> I have a typehandler for the enum in the second result map which is
> referenced from the CounterpartyHedgeVOResult result map.
> 
> Do you mean that I should try having the second result map just return
> strings, and change the line in the first result map to this? :
> 
> <result resultMap="CDS_HEDGE.CreditEventEnumResult" property="creditEvents"
> typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
> 
> I'll give it a go tomorrow when I'm at work.
> 
> Thanks,
>  micke
> 
> 2009/5/18 DelGurth <de...@gmail.com>
> 
>> On Mon, May 18, 2009 at 7:40 PM, Mikael Andersson <ma...@gmail.com>
>> wrote:
>> > Hi
>> >
>> > I'm having problems mapping to a property which is a Set of java5 enums.
>> >
>> > I have a class CounterpartyHedgeVO which has the property creditEvents
>> which
>> > is a Set<CreditEventEnum>.
>> >
>> > Not sure what the problem is but I'm guessing that iBATIS tries to treat
>> the
>> > Enum like a POJO and fails to create it since there isn't a public
>> > constructor. Or?
>> >
>> > Any help would be really appreciated.
>> >
>> > My current sqlmap (omitted the typeAlias definitions):
>> >
>> > <resultMap class="CounterpartyHedgeVO" id="CounterpartyHedgeVOResult"
>> > groupBy="refNo">
>> >     <result column="POS_RefNoADB" jdbcType="VARCHAR" property="refNo"/>
>> >    <result column="PRT_Code" jdbcType="VARCHAR" property="prtCode"/>
>> >       <result column="RBE_CodeCpty" jdbcType="VARCHAR"
>> > property="counterpartyCode"/>
>> >       <result column="POS_Tradebook"  jdbcType="VARCHAR"
>> > property="tradeBook"/>
>> >
>> >       <result resultMap="CDS_HEDGE.CreditEventEnumResult"
>> > property="creditEvents"/>
>> >  </resultMap>
>> >
>> >  <resultMap class="CreditEventEnum" id="CreditEventEnumResult">
>> >   <result column="CRE_EventType" property="value" jdbcType="VARCHAR"
>> > typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
>> >  </resultMap>
>> >
>> > Stack trace:
>> > --- The error occurred in ibatis/sqlmap-map-cds-hedge.xml.
>> > --- The error occurred while applying a result map.
>> > --- Check the CDS_HEDGE.CreditEventEnumResult.
>> > --- The error occured while instantiating the result object
>> > --- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not
>> > instantiate result class.  Cause: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:615)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:589)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
>> >  at
>> >
>> org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
>> >  at
>> >
>> org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
>> >  ... 40 more
>> > Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not
>> > instantiate result class.  Cause: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at
>> >
>> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue(BasicResultMap.java:449)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap(BasicResultMap.java:396)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:382)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQueryProcedure(SqlExecutor.java:283)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.sqlExecuteQuery(ProcedureStatement.java:34)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
>> >  ... 46 more
>> > Caused by: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at java.lang.Class.newInstance0(Class.java:340)
>> >  at java.lang.Class.newInstance(Class.java:308)
>> >  at com.ibatis.common.resources.Resources.instantiate(Resources.java:294)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:127)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:95)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
>> >  ... 56 more
>> >
>> > Cheers,
>> >  Micke
>>
>> I think you will need to create a typehandler so iBATIS can work with your
>> Enum.
>>
>> See:
>> http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration
>>
>> Regards,
>> Wessel
>>
> 


Re: Mapping of a Set with enums

Posted by AsafM <as...@gmail.com>.
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: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Mapping of a Set with enums

Posted by Mike_ <ma...@gmail.com>.
Hi

Thanks for the help!

Having the secondMap return strings and moving the typehandler to the result
element in the first resultMap worked perfectly. 
Just not very easy to figure out, I've done it the way I initially tried
with JavaBeans before which worked fine.

Thanks again,
 Micke


Mike_ wrote:
> 
> Hi
> 
> I have a typehandler for the enum in the second result map which is
> referenced from the CounterpartyHedgeVOResult result map.
> 
> Do you mean that I should try having the second result map just return
> strings, and change the line in the first result map to this? :
> 
> <result resultMap="CDS_HEDGE.CreditEventEnumResult"
> property="creditEvents"
> typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
> 
> I'll give it a go tomorrow when I'm at work.
> 
> Thanks,
>  micke
> 
> 2009/5/18 DelGurth <de...@gmail.com>
> 
>> On Mon, May 18, 2009 at 7:40 PM, Mikael Andersson <ma...@gmail.com>
>> wrote:
>> > Hi
>> >
>> > I'm having problems mapping to a property which is a Set of java5
>> enums.
>> >
>> > I have a class CounterpartyHedgeVO which has the property creditEvents
>> which
>> > is a Set<CreditEventEnum>.
>> >
>> > Not sure what the problem is but I'm guessing that iBATIS tries to
>> treat
>> the
>> > Enum like a POJO and fails to create it since there isn't a public
>> > constructor. Or?
>> >
>> > Any help would be really appreciated.
>> >
>> > My current sqlmap (omitted the typeAlias definitions):
>> >
>> > <resultMap class="CounterpartyHedgeVO" id="CounterpartyHedgeVOResult"
>> > groupBy="refNo">
>> >     <result column="POS_RefNoADB" jdbcType="VARCHAR" property="refNo"/>
>> >    <result column="PRT_Code" jdbcType="VARCHAR" property="prtCode"/>
>> >       <result column="RBE_CodeCpty" jdbcType="VARCHAR"
>> > property="counterpartyCode"/>
>> >       <result column="POS_Tradebook"  jdbcType="VARCHAR"
>> > property="tradeBook"/>
>> >
>> >       <result resultMap="CDS_HEDGE.CreditEventEnumResult"
>> > property="creditEvents"/>
>> >  </resultMap>
>> >
>> >  <resultMap class="CreditEventEnum" id="CreditEventEnumResult">
>> >   <result column="CRE_EventType" property="value" jdbcType="VARCHAR"
>> > typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
>> >  </resultMap>
>> >
>> > Stack trace:
>> > --- The error occurred in ibatis/sqlmap-map-cds-hedge.xml.
>> > --- The error occurred while applying a result map.
>> > --- Check the CDS_HEDGE.CreditEventEnumResult.
>> > --- The error occured while instantiating the result object
>> > --- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not
>> > instantiate result class.  Cause: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:615)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:589)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
>> >  at
>> >
>> org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
>> >  at
>> >
>> org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
>> >  ... 40 more
>> > Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not
>> > instantiate result class.  Cause: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at
>> >
>> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue(BasicResultMap.java:449)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap(BasicResultMap.java:396)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:382)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQueryProcedure(SqlExecutor.java:283)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.sqlExecuteQuery(ProcedureStatement.java:34)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
>> >  ... 46 more
>> > Caused by: java.lang.InstantiationException:
>> > hedge.interfaces.CreditEventEnum
>> >  at java.lang.Class.newInstance0(Class.java:340)
>> >  at java.lang.Class.newInstance(Class.java:308)
>> >  at
>> com.ibatis.common.resources.Resources.instantiate(Resources.java:294)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:127)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:95)
>> >  at
>> >
>> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
>> >  ... 56 more
>> >
>> > Cheers,
>> >  Micke
>>
>> I think you will need to create a typehandler so iBATIS can work with
>> your
>> Enum.
>>
>> See:
>> http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration
>>
>> Regards,
>> Wessel
>>
> 
> 

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


Re: Mapping of a Set with enums

Posted by Mikael Andersson <ma...@gmail.com>.
Hi

I have a typehandler for the enum in the second result map which is
referenced from the CounterpartyHedgeVOResult result map.

Do you mean that I should try having the second result map just return
strings, and change the line in the first result map to this? :

<result resultMap="CDS_HEDGE.CreditEventEnumResult" property="creditEvents"
typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>

I'll give it a go tomorrow when I'm at work.

Thanks,
 micke

2009/5/18 DelGurth <de...@gmail.com>

> On Mon, May 18, 2009 at 7:40 PM, Mikael Andersson <ma...@gmail.com>
> wrote:
> > Hi
> >
> > I'm having problems mapping to a property which is a Set of java5 enums.
> >
> > I have a class CounterpartyHedgeVO which has the property creditEvents
> which
> > is a Set<CreditEventEnum>.
> >
> > Not sure what the problem is but I'm guessing that iBATIS tries to treat
> the
> > Enum like a POJO and fails to create it since there isn't a public
> > constructor. Or?
> >
> > Any help would be really appreciated.
> >
> > My current sqlmap (omitted the typeAlias definitions):
> >
> > <resultMap class="CounterpartyHedgeVO" id="CounterpartyHedgeVOResult"
> > groupBy="refNo">
> >     <result column="POS_RefNoADB" jdbcType="VARCHAR" property="refNo"/>
> >    <result column="PRT_Code" jdbcType="VARCHAR" property="prtCode"/>
> >       <result column="RBE_CodeCpty" jdbcType="VARCHAR"
> > property="counterpartyCode"/>
> >       <result column="POS_Tradebook"  jdbcType="VARCHAR"
> > property="tradeBook"/>
> >
> >       <result resultMap="CDS_HEDGE.CreditEventEnumResult"
> > property="creditEvents"/>
> >  </resultMap>
> >
> >  <resultMap class="CreditEventEnum" id="CreditEventEnumResult">
> >   <result column="CRE_EventType" property="value" jdbcType="VARCHAR"
> > typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
> >  </resultMap>
> >
> > Stack trace:
> > --- The error occurred in ibatis/sqlmap-map-cds-hedge.xml.
> > --- The error occurred while applying a result map.
> > --- Check the CDS_HEDGE.CreditEventEnumResult.
> > --- The error occured while instantiating the result object
> > --- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not
> > instantiate result class.  Cause: java.lang.InstantiationException:
> > hedge.interfaces.CreditEventEnum
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
> >  at
> >
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:615)
> >  at
> >
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:589)
> >  at
> >
> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
> >  at
> >
> org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
> >  at
> >
> org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
> >  ... 40 more
> > Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not
> > instantiate result class.  Cause: java.lang.InstantiationException:
> > hedge.interfaces.CreditEventEnum
> >  at
> >
> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue(BasicResultMap.java:449)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap(BasicResultMap.java:396)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:382)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
> >  at
> >
> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
> >  at
> >
> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
> >  at
> >
> com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQueryProcedure(SqlExecutor.java:283)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.sqlExecuteQuery(ProcedureStatement.java:34)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
> >  ... 46 more
> > Caused by: java.lang.InstantiationException:
> > hedge.interfaces.CreditEventEnum
> >  at java.lang.Class.newInstance0(Class.java:340)
> >  at java.lang.Class.newInstance(Class.java:308)
> >  at com.ibatis.common.resources.Resources.instantiate(Resources.java:294)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:127)
> >  at
> >
> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:95)
> >  at
> >
> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
> >  ... 56 more
> >
> > Cheers,
> >  Micke
>
> I think you will need to create a typehandler so iBATIS can work with your
> Enum.
>
> See:
> http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration
>
> Regards,
> Wessel
>

Re: Mapping of a Set with enums

Posted by DelGurth <de...@gmail.com>.
On Mon, May 18, 2009 at 7:40 PM, Mikael Andersson <ma...@gmail.com> wrote:
> Hi
>
> I'm having problems mapping to a property which is a Set of java5 enums.
>
> I have a class CounterpartyHedgeVO which has the property creditEvents which
> is a Set<CreditEventEnum>.
>
> Not sure what the problem is but I'm guessing that iBATIS tries to treat the
> Enum like a POJO and fails to create it since there isn't a public
> constructor. Or?
>
> Any help would be really appreciated.
>
> My current sqlmap (omitted the typeAlias definitions):
>
> <resultMap class="CounterpartyHedgeVO" id="CounterpartyHedgeVOResult"
> groupBy="refNo">
>     <result column="POS_RefNoADB" jdbcType="VARCHAR" property="refNo"/>
>    <result column="PRT_Code" jdbcType="VARCHAR" property="prtCode"/>
>       <result column="RBE_CodeCpty" jdbcType="VARCHAR"
> property="counterpartyCode"/>
>       <result column="POS_Tradebook"  jdbcType="VARCHAR"
> property="tradeBook"/>
>
>       <result resultMap="CDS_HEDGE.CreditEventEnumResult"
> property="creditEvents"/>
>  </resultMap>
>
>  <resultMap class="CreditEventEnum" id="CreditEventEnumResult">
>   <result column="CRE_EventType" property="value" jdbcType="VARCHAR"
> typeHandler="hedge.dao.CreditEventEnumReadOnlyTypeHandler"/>
>  </resultMap>
>
> Stack trace:
> --- The error occurred in ibatis/sqlmap-map-cds-hedge.xml.
> --- The error occurred while applying a result map.
> --- Check the CDS_HEDGE.CreditEventEnumResult.
> --- The error occured while instantiating the result object
> --- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not
> instantiate result class.  Cause: java.lang.InstantiationException:
> hedge.interfaces.CreditEventEnum
>  at
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
>  at
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
>  at
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:615)
>  at
> com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:589)
>  at
> com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
>  at
> org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
>  at
> org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
>  ... 40 more
> Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not
> instantiate result class.  Cause: java.lang.InstantiationException:
> hedge.interfaces.CreditEventEnum
>  at
> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
>  at
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:373)
>  at
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue(BasicResultMap.java:449)
>  at
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap(BasicResultMap.java:396)
>  at
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:382)
>  at
> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
>  at
> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:382)
>  at
> com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:301)
>  at
> com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQueryProcedure(SqlExecutor.java:283)
>  at
> com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.sqlExecuteQuery(ProcedureStatement.java:34)
>  at
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
>  ... 46 more
> Caused by: java.lang.InstantiationException:
> hedge.interfaces.CreditEventEnum
>  at java.lang.Class.newInstance0(Class.java:340)
>  at java.lang.Class.newInstance(Class.java:308)
>  at com.ibatis.common.resources.Resources.instantiate(Resources.java:294)
>  at
> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:127)
>  at
> com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:95)
>  at
> com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
>  ... 56 more
>
> Cheers,
>  Micke

I think you will need to create a typehandler so iBATIS can work with your Enum.

See: http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration

Regards,
Wessel