我想知道是否可以在 JPA 中使用一元运算符设置 bool 值。 我的意思是这样的
@Modifying
@Query("update Computer com set com.enabled=!com.enabled where com.id = ?1")
启用字段在POJO中是这样映射的
private Boolean enabled;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
在数据库中,它存储为 boolean(1)
这是结果
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: expecting '=', found 'c' [update com.nicinc.Computer com set com.enabled=!com.enabled where com.id = ?1]
这里是 JPA 属性
Spring Data JPA 属性
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.H2Dialect
最佳答案
不同的数据库对 bool 表达式的处理有不同的支持,所以 JPA 提供通用方法的空间很小,因此你必须明确:
update Computer
set enabled = case when enabled = true then false else true end
where id = ?1
关于mysql - JPA 一元运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563550/