将boolean设置为javax.persistenc.Query

时间:2017-10-20 10:18:09

标签: hibernate jpa

我们不使用hibernate propriety API。 我们遵循纯JPA标准API。 如何设置一个布尔值作为JPQL查询的参数?(javax.persistenc.Query)  以下是行不通的

    @NamedQuery(name = "Employee.getEmployeeByState", query = "select employee.id From Employee employee where employee.state=:inputState")


    Query getEmployeesExcluded = entityManager.createNamedQuery("Employee.getEmployeeByState");
    getEmployeesExcluded .setParameter("inputState", Boolean.FALSE);

1 个答案:

答案 0 :(得分:-1)

问题可能发生,因为在您的实体中,您应该将state属性设为boolean,但在您的数据库中应该是tinyintint或类似的内容(因为我们不知道你所假设的实体),所以你应该使用typeConverter

@Entity
@NamedQuery(name = "Employee.getEmployeeByState", query = "select employee.id From Employee employee where employee.state=:inputState")
public class Employee implements Serializable {

    @TypeConverter(name="intToBoolean", dataType=Integer.class, objectType=Boolean.class)
    private boolean state;

    //getters and setters
}

here您可以找到有关该注释的更多信息。

修改

或者您可以实施自己的转换器,查看thisthisthis