Java 更新查询spring数据JPA修改所有列
我正在尝试根据用户从UI传递的Id更新表 问题陈述示例:当用户将Id作为1发送时,应仅使用其他几个参数(如lastModifiedBy和Delete)更新表中的一行,其中“我的代码”将更新表中的所有行 orm.xmlJava 更新查询spring数据JPA修改所有列,java,postgresql,spring-boot,java-8,spring-data-jpa,Java,Postgresql,Spring Boot,Java 8,Spring Data Jpa,我正在尝试根据用户从UI传递的Id更新表 问题陈述示例:当用户将Id作为1发送时,应仅使用其他几个参数(如lastModifiedBy和Delete)更新表中的一行,其中“我的代码”将更新表中的所有行 orm.xml <entity class="com.example.Domain" name="Domain"> <table name="t_table"/> <attributes> <id name="id">
<entity class="com.example.Domain" name="Domain">
<table name="t_table"/>
<attributes>
<id name="id">
<column name="ID" nullable="false"/>
<generated-value strategy="SEQUENCE" generator="S_GEN"/>
<sequence-generator name="S_GEN" sequence-name="S_GEN" allocation-size="1"/>
</id>
<basic name="deleted">
<column name="DELETED" nullable="false" length="5"/>
</basic>
<basic name="lastModifiedBy">
<column name="LAST_MODIFIED_BY" nullable="false" length="100"/>
</basic>
</attributes>
JPA存储库:
public interface JpaDomainRepository extends Repository<Domain, Long> {
@Modifying
@Query(value = "update Domain set DELETED = true, LAST_MODIFIED_BY = lastModifiedBy where id = id")
int update(Domain domain);
}
若表已经有多个条目,并且我正在尝试更新第一行,那个么其余所有行都将被修改
简而言之,对于下面的where子句,我的update查询始终为true
例如,对于输入1,只有id=1的一列应该得到修改,但查询也在修改第2行、第3行等等
update Domain set DELETED = true, LAST_MODIFIED_BY = lastModifiedBy where 1= 1
为什么会这样?解决方案是什么
附言:
我不想从服务类显式传递ID,因为我的ID在域对象中已经可用。其中ID=ID将始终为true,如果您使用的正是这种语法。。。你是说别的吗 @Query中的参数不应该前面加上?对于索引参数和:对于命名参数
@Query(value = "update Domain set DELETED = true, LAST_MODIFIED_BY = :lastModifiedBy where id = :id")
我已在域对象中具有id。1.传递未使用的域对象的意义是什么:为什么不简单地传递ID?2.为什么您要编写一个查询来执行更新,而不是使用标准ORM功能,在这里您可以通过继承的
save(entity)
方法进行更新,如果您扩展了crudepository
而不是tahnRepository
@AlanHay 1,您将可以使用该方法。拥有域对象的点是2。我之所以编写查询,是因为我使用的是spring数据jpa.1。您没有编写本机查询:这是一个JPQL查询。2.拥有域对象的点是……——我认为您需要回顾一下JPA的一些基本概念。@AlanHay传递完整的域对象而不是传递多个参数,这不是一个好方法吗?我希望您理解我的问题陈述,如果您在更新方法中实际使用它,那将是一个问题。但你不;(我正在寻找SpEL解决方案,并在该URL中找到
update Domain set DELETED = true, LAST_MODIFIED_BY = lastModifiedBy where 1= 1
@Query(value = "update Domain set DELETED = true, LAST_MODIFIED_BY = :lastModifiedBy where id = :id")