I'm trying to update table based on Id that user passes from UI.
Problem statement example : when user sends Id as 1 then table should be updated only one row with other few parameters like lastModifiedBy and deleted where as my code updates all rows in table,
orm.xml
<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>
Entity class
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Domain {
private Long id;
private boolean deleted;
private String lastModifiedBy;
}
JPA Repository:
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);
}
Service Class
public void update(UpdateCommand command) {
Domain domain = repository.findByIdAndDeletedFalse(command.getID());
domain.setLastModifiedOn(command.getLastModifiedOn());
domain.setLastModifiedBy(command.getLastModifiedBy());
repository.update(domain);
}
If table is having multiple entries already and I'm trying to update first row then rest all rows are getting modified,
In-short notation my update query is always true for where clause like below,
Example for input 1, only one column where id = 1 should get modify but query is modifying rows 2, 3 and so on as well,
update Domain set DELETED = true, LAST_MODIFIED_BY = lastModifiedBy where 1= 1
Why is it behaving like so and what could be solution?
PS: I don't want to pass ID explicitly from service class since I id is already available in domain object.