Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter value [1] did not match expected type [java.lang.Boolean]

Tags:

java

spring

hql

I'm getting the error Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [1] did not match expected type [java.lang.Boolean]; nested exception is java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [java.lang.Boolean].

I'm confused by this because it's coming from the service method shown below that is commented out. When I comment it out, the error is avoided. The active column is a TINYINT(1) that's either 1 or 0.

Entity:

@Entity
@NamedQueries({
        @NamedQuery(name="Workflow.findByUUID", query="SELECT w FROM Workflow w WHERE w.uuid = :uuid"),
        @NamedQuery(name="Workflow.findByActive", query="SELECT w FROM Workflow w WHERE w.active = :active ORDER BY id ASC")
})

My Repository:

@Repository
public interface WorkflowRepository extends JpaRepository<Workflow, Integer> {
    List<Workflow> findByActive(@Param("active") Integer active);
}

My Service:

@Service
public class WorkflowService {

    @Autowired
    WorkflowRepository workflowRepository;

    /**
     * Get active workflows
     */
    @Transactional(readOnly = true)
    public List<Workflow> findActive() {
        //return workflowRepository.findByActive(1);
        return null;
    }

When I uncomment

like image 480
Webnet Avatar asked Apr 03 '13 15:04

Webnet


1 Answers

You seem to have mapped Workflow.active attribute as a Boolean. So you repository should be like:

@Repository
public interface WorkflowRepository extends JpaRepository<Workflow, Boolean> {
    List<Workflow> findByActive(@Param("active") Boolean active);
}

And the call workflowRepository.findByActive(true) should behave how you want it to.

The fact is that HQL sometimes make a type difference between database and Java mapping, because typing isn't the same in these environments. So check your Entity's active type to make the appropriate Repository.

like image 101
ngasull Avatar answered Oct 15 '22 17:10

ngasull