[mybatis guice] adding objectfactory?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[mybatis guice] adding objectfactory?

nino martinez wael
Hi

I have a case where I have an object returned from a select statement that is a little complex. It has an enum on it which needs to be set based on some logic converting from int to the enum. So how would I do this in general with mybatis and how is it supposed to be done with mybatis guice?

regards Nino
Reply | Threaded
Open this post in threaded view
|

Re: [mybatis guice] adding objectfactory?

nino martinez wael
hmm mayby it's just an typehandler I need..?

2010/10/13 nino martinez wael <[hidden email]>
Hi

I have a case where I have an object returned from a select statement that is a little complex. It has an enum on it which needs to be set based on some logic converting from int to the enum. So how would I do this in general with mybatis and how is it supposed to be done with mybatis guice?

regards Nino

Reply | Threaded
Open this post in threaded view
|

Re: [mybatis guice] adding objectfactory?

Guy Rouillier
On 10/13/2010 9:30 AM, nino martinez wael wrote:
> hmm mayby it's just an typehandler I need..?

Yes.  In the valueOf() method of your TypeHandler, simply convert the
incoming int to your Java enum.

--
Guy Rouillier
Reply | Threaded
Open this post in threaded view
|

Re: [mybatis guice] adding objectfactory?

nino martinez wael
Yup, and thanks :)

2010/10/14 Guy Rouillier <[hidden email]>
On 10/13/2010 9:30 AM, nino martinez wael wrote:
hmm mayby it's just an typehandler I need..?

Yes.  In the valueOf() method of your TypeHandler, simply convert the incoming int to your Java enum.

--
Guy Rouillier

Reply | Threaded
Open this post in threaded view
|

Re: [mybatis guice] adding objectfactory?

nino martinez wael
hmm does not seem to work. I get three results as expected in the list, however all are null. I debuged and saw that the typehandler are never activated.


Wondering what I have done wrong:

AgentMapper:
    @Select("SELECT x.resourceName, t.eventType, x.datetime FROM (SELECT t1.resourceID, t1.resourceName, MAX(t2.eventDateTime) AS datetime FROM Resource AS t1 INNER JOIN AgentStateDetail AS t2 ON t2.agentID = t1.resourceID GROUP BY t1.resourceID, t1.resourceName ) AS x INNER JOIN AgentStateDetail AS t ON t.agentID = x.resourceID AND t.eventDateTime = x.datetime ORDER BY x.resourceName")
    public List<Agent> selectAllAgents();



Guice config:

        MyBatisModule myBatisModule = new MyBatisModule(
                DataSourceProvider.class, JdbcTransactionFactoryProvider.class);
        myBatisModule.addMapperClasses(CiscoStatisticsMapper.class);

        myBatisModule.addTypeHandler(AgentStatus.class,
                AgentStatusTypeHandler.class);
        myBatisModule.addMapperClasses(AgentMapper.class);
        Names.bindProperties(
                this.binder(),
                getConnectionPropertiesAndSetupCache(CacheManager.getInstance()));

        install(myBatisModule);

Type Handler:
public class AgentStatusTypeHandler extends BaseTypeHandler implements
        TypeHandler {

    public void setNonNullParameter(PreparedStatement ps, int i,
            Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.toString());
    }

    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        int status = rs.getInt(columnName);
        return convert(status);
    }

    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        int status = cs.getInt(columnIndex);
        return convert(status);
    }

    private AgentStatus convert(int statusInt) {
        AgentStatus status = null;
        switch (statusInt) {
        case 1:
            status = AgentStatus.LoggedIn;
            break;
        case 2:
            status = AgentStatus.NotReady;
            break;
        case 3:
            status = AgentStatus.Ready;
            break;
        case 4:
            status = AgentStatus.Reserved;
            break;
        case 5:
            status = AgentStatus.Talking;
            break;
        case 6:
            status = AgentStatus.Working;
            break;
        case 7:
            status = AgentStatus.LoggedOut;
            break;

        default:
            break;
        }
        return status;

    }


2010/10/14 nino martinez wael <[hidden email]>
Yup, and thanks :)

2010/10/14 Guy Rouillier <[hidden email]>

On 10/13/2010 9:30 AM, nino martinez wael wrote:
hmm mayby it's just an typehandler I need..?

Yes.  In the valueOf() method of your TypeHandler, simply convert the incoming int to your Java enum.

--
Guy Rouillier


Reply | Threaded
Open this post in threaded view
|

Re: [mybatis guice] adding objectfactory?

nino martinez wael
my bad wrong query..

2010/10/14 nino martinez wael <[hidden email]>
hmm does not seem to work. I get three results as expected in the list, however all are null. I debuged and saw that the typehandler are never activated.


Wondering what I have done wrong:

AgentMapper:
    @Select("SELECT x.resourceName, t.eventType, x.datetime FROM (SELECT t1.resourceID, t1.resourceName, MAX(t2.eventDateTime) AS datetime FROM Resource AS t1 INNER JOIN AgentStateDetail AS t2 ON t2.agentID = t1.resourceID GROUP BY t1.resourceID, t1.resourceName ) AS x INNER JOIN AgentStateDetail AS t ON t.agentID = x.resourceID AND t.eventDateTime = x.datetime ORDER BY x.resourceName")
    public List<Agent> selectAllAgents();



Guice config:

        MyBatisModule myBatisModule = new MyBatisModule(
                DataSourceProvider.class, JdbcTransactionFactoryProvider.class);
        myBatisModule.addMapperClasses(CiscoStatisticsMapper.class);

        myBatisModule.addTypeHandler(AgentStatus.class,
                AgentStatusTypeHandler.class);
        myBatisModule.addMapperClasses(AgentMapper.class);
        Names.bindProperties(
                this.binder(),
                getConnectionPropertiesAndSetupCache(CacheManager.getInstance()));

        install(myBatisModule);

Type Handler:
public class AgentStatusTypeHandler extends BaseTypeHandler implements
        TypeHandler {

    public void setNonNullParameter(PreparedStatement ps, int i,
            Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.toString());
    }

    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        int status = rs.getInt(columnName);
        return convert(status);
    }

    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        int status = cs.getInt(columnIndex);
        return convert(status);
    }

    private AgentStatus convert(int statusInt) {
        AgentStatus status = null;
        switch (statusInt) {
        case 1:
            status = AgentStatus.LoggedIn;
            break;
        case 2:
            status = AgentStatus.NotReady;
            break;
        case 3:
            status = AgentStatus.Ready;
            break;
        case 4:
            status = AgentStatus.Reserved;
            break;
        case 5:
            status = AgentStatus.Talking;
            break;
        case 6:
            status = AgentStatus.Working;
            break;
        case 7:
            status = AgentStatus.LoggedOut;
            break;

        default:
            break;
        }
        return status;

    }


2010/10/14 nino martinez wael <[hidden email]>

Yup, and thanks :)

2010/10/14 Guy Rouillier <[hidden email]>

On 10/13/2010 9:30 AM, nino martinez wael wrote:
hmm mayby it's just an typehandler I need..?

Yes.  In the valueOf() method of your TypeHandler, simply convert the incoming int to your Java enum.

--
Guy Rouillier