I have tried and ask the same question on StackOverFlow but it seem no help. [ https://stackoverflow.com/questions/57283004/dynamicupdate-added-extra-untouched-column-in-update-statement?noredirect=1#comment101061876_57283004 |https://stackoverflow.com/questions/57283004/dynamicupdate-added-extra-untouched-column-in-update-statement?noredirect=1#comment101061876_57283004] I think there is some problem down the code but not very sure how to really pinpoint the problem.
I am facing a weird behavior using @DynamicUpdate with one of my entity. So my entity is defined like this ```java
{code:java} @Entity @DynamicUpdate @Table(name = "courts") @Getter @Setter @Builder @AllArgsConstructor // require for @Builder to work correctly @NoArgsConstructor // required for hibernate mapping public class CourtDO {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
// name is SQL keyword @Column(name = "court_name") private String name;
@Column private String addressStreet;
@Column private String addressWard;
@Column private String addressDistrict;
@Column private String addressCity;
@Column private String addressCountry;
@Column private String phoneNumber;
@Column(nullable = false) @Convert(converter = DOTimestampConverter.class) @CreationTimestamp private ZonedDateTime createdAt;
@Column(nullable = false) @Convert(converter = DOTimestampConverter.class) @UpdateTimestamp private ZonedDateTime updatedAt; } {code} ``` `DOTimestampConverter` is just a simple `AttributeConverter` to convert `ZonedDateTime` to ms for me to store in DB as number. As you can see I marked the entity with `@DynamicUpdate`.
I have a small jersey REST-API which allow me to update all of the normal field beside the id and the generated date. I always check if the input is `null` before actually touching the setter for the field.
I see a very weird behavior with the column `address_city`, it would be included in every update like this even if I only touch other fields, in this case, just the `name` which translate to `court_name` because `name` is a reserved keyword in sql. **UPDATE** Actually the problem is not with just the `address_city` collumn. Even if I ignore that column, other column would get included in the update statement. ``` {code} Hibernate: update courts set address_city=?, court_name=?, updated_at=? where id=? ``` {code}
Where as if I only edit the `address_city` only then I would see it correctly ``` {code} Hibernate: update courts set address_city=?, updated_at=? where id=? {code} ``` Did anyone face this problem before? The stack I am using are
``` spring-boot 2.1.3.RELEASE spring-boot-starter-data-jpa postgresql 11.4 ```
And the data schema ```sql {code:sql} CREATE TABLE COURTS ( id SERIAL PRIMARY KEY, court_name VARCHAR(20), address_street VARCHAR(128), address_ward VARCHAR(20), address_district VARCHAR(20), address_city VARCHAR(20), address_country VARCHAR(10), phone_number VARCHAR(20), created_at BIGINT NOT NULL, updated_at BIGINT NOT NULL ); {code} ``` This is how update through REST-API ``` {code:java} @Override public Court editCourt(String courtId, CreateCourtRequest createCourtRequest) { Optional<CourtDO> court = courtRepository.findById(NumberUtils.toLong(courtId));
return court .map(courtDO -> editCourtInfo(courtDO, createCourtRequest)) .map(courtRepository::save) .map(courtDOToResponseConverter::convert) .orElse(null); }
private CourtDO editCourtInfo(CourtDO courtDO, CreateCourtRequest createCourtRequest) { if (StringUtils.isNotBlank(createCourtRequest.getName())) { courtDO.setName(createCourtRequest.getName()); }
if (StringUtils.isNotBlank(createCourtRequest.getAddressStreet())) { courtDO.setAddressStreet(createCourtRequest.getAddressStreet()); }
if (StringUtils.isNotBlank(createCourtRequest.getAddressWard())) { courtDO.setAddressWard(createCourtRequest.getAddressWard()); }
if (StringUtils.isNotBlank(createCourtRequest.getAddressDistrict())) { courtDO.setAddressDistrict(createCourtRequest.getAddressDistrict()); }
if (StringUtils.isNotBlank(createCourtRequest.getAddressCity())) { courtDO.setAddressCity(createCourtRequest.getAddressCity()); }
if (StringUtils.isNotBlank(createCourtRequest.getPhoneNumber())) { courtDO.setPhoneNumber(createCourtRequest.getPhoneNumber()); }
return courtDO; } {code} ``` |
|