Java/My Batis not returning boolean?

7.2k views Asked by At

I have this chunk in the controller:

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public boolean isWorkflowCoordinatorExistingForStep(final long workflowStepId) {
    Validate.notNull(workflowStepId, "workflow step id cannot be null");
    return (boolean) this.auditingSqlSession.selectOne("isWorkflowCoordinatorExistingForStep", workflowStepId);
}

That maps with this:

<select id="isWorkflowCoordinatorExistingForStep" resultType="java.lang.Boolean">
        SELECT is_coordinator
        FROM mo_subject_privileges
        WHERE workflow_step_id = #{workflowStepId}


</select>

It seems fine to me, but it is not: I am getting a "Inconvertible types. Cannot cast java.lang.Object to boolean"

Any hint?

3

There are 3 answers

2
duffy356 On

Does your Database support Booleans?

For instance Oracle has no native Boolean, so it might be possible, that mybatis does not support Booleans.

Try something else: Select the row and then check in your DAO-Method if the special field is a boolean.

DAO:

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public boolean isWorkflowCoordinatorExistingForStep(final long workflowStepId) {
    Validate.notNull(workflowStepId, "workflow step id cannot be null");
    SomeObject so = (SomeObject) this.auditingSqlSession.selectOne("isWorkflowCoordinatorExistingForStep", workflowStepId);
    return so.isCoordinator(); // if neccassary, check for null
}

XML-Map:

<select id="isWorkflowCoordinatorExistingForStep" resultType="path.to.SomeObject">
        SELECT *
        FROM mo_subject_privileges
        WHERE workflow_step_id = #{workflowStepId}   
</select>
0
kingfrito_5005 On

Its possible that the data is stored in your Database as a string rather than a boolean, as is often the case. Maybe add a println(selectone.tostring()) to make sure that the output is 'TRUE' and not 'T' for example.

0
javalearner_heaven On

You check whether the field is String in DB. You can decode the value

DECODE(is_coordinator,'t', true, 'f', false)