gpt4 book ai didi

java - 由于破坏查询的条件,来自 org.springframework.data.mybatis.annotations.Version 的 @Version 在更新时不会增加 1

转载 作者:行者123 更新时间:2023-12-01 17:29:53 24 4
gpt4 key购买 nike

描述

在 Spring Boot 1.5.9 应用程序中,

我在 version 属性上的 @Entity 中使用 org.springframework.data.mybatis.annotations.Version 注释来获取版本每次更新都会增加,但它不断破坏我的应用程序。

错误是:

 update effect 0 row, maybe version control lock occurred.

我可以看到请求以以下内容结尾:

"version"="version"+1,
where
"id"=21
and "version"=null

和 "version"=null 导致整个请求编辑 0 行

这是实体的示例:


CREATE TABLE "cm_company_postal_address"
(
"id" BIGSERIAL NOT NULL,
"note" VARCHAR(255) DEFAULT NULL,
"city" VARCHAR(255) NOT NULL,
"postal_code" BIGINT DEFAULT NULL,
"c_ref_country_id" BIGINT NOT NULL,
"cm_company_id" BIGINT NOT NULL,
"street_address_line1" VARCHAR NOT NULL,
"street_address_line2" VARCHAR DEFAULT NULL,
"post_box" VARCHAR(20) DEFAULT NULL,
"version" BIGINT DEFAULT NULL,
"created_date_time" TIMESTAMPTZ DEFAULT NULL,
"created_by_id" BIGINT DEFAULT NULL,
"last_modified_date_time" TIMESTAMPTZ DEFAULT NULL,
"last_modified_by_id" BIGINT DEFAULT NULL,
"deleted" BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id"),
CONSTRAINT company_id_fk FOREIGN KEY ("cm_company_id") REFERENCES "cm_company" ("id")
);

这是java类:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.mybatis.annotations.Column;
import org.springframework.data.mybatis.annotations.Condition;
import org.springframework.data.mybatis.annotations.Conditions;
import org.springframework.data.mybatis.annotations.Entity;
import static org.springframework.data.repository.query.parser.Part.Type.CONTAINING;

@Data
@JsonIgnoreProperties("new")
@Entity(table = "cm_company_postal_address")
@EqualsAndHashCode(callSuper = false)
public class PostalAddress {
@Id(strategy = AUTO)
private Long id;
private String description;
@Version
private Integer version;
@CreatedDate
@JsonUnwrapped
@JdbcType(TIMESTAMP)
private Instant createdDateTime;
@LastModifiedDate
@JsonUnwrapped
@JdbcType(TIMESTAMP)
private Instant lastModifiedDateTime;
@CreatedBy
private Long createdById;
@LastModifiedBy
private Long lastModifiedById;
@JsonProperty(value = "deleted")
private Boolean deleted = false;
@Column(name = "cm_company_id")
private Long companyId;
private String city;
private Long postalCode;
private Long countryId;
private String streetAddressLine1;
private String streetAddressLine2;
private String postBox;

}

这是我所呈现实体的@Repository:

import com.kopaxgroup.api.companyManagement.domain.postalAddress.geography.PostalAddress;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.data.mybatis.repository.support.MybatisRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(exported = false)
public interface PostalAddressRepository extends MybatisRepository<PostalAddress, Long> {

}

这是同一实体的服务接口(interface):

import com.kopaxgroup.api.companyManagement.domain.postalAddress.geography.PostalAddress;
import org.springframework.data.support.CrudService;


public interface PostalAddressService extends CrudService<PostalAddress, Long> {

}

它的实现:

import com.kopaxgroup.api.companyManagement.domain.postalAddress.geography.PostalAddress;
import com.kopaxgroup.api.companyManagement.repository.PostalAddressRepository;
import com.kopaxgroup.api.companyManagement.service.PostalAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.support.AbstractCrudService;
import org.springframework.stereotype.Service;

@Service
public class PostalAddressServiceImpl extends AbstractCrudService<PostalAddressRepository, PostalAddress, Long> implements PostalAddressService {

@Autowired
public PostalAddressServiceImpl(PostalAddressRepository repository) {
super(repository);
}

这就是我更新实体的方式:

import com.kopaxgroup.api.companyManagement.domain.postalAddress.geography.PostalAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/postal-address")
public class PostalAddressController {

@Autowired
private PostalAddressService postalAddressService;

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
void modify(@PathVariable("id") Long id, @RequestBody PostalAddress entity) {
entity.setId(id);
postalAddressService.updateIgnore(entity);
}
}

预期

我希望该实体的 version 列在每次更新时都会增加。

结果

相反,每次更新都会为版本列保留一个 null 值。

问题

  • 如何防止 spring-data-mybatis 在请求末尾附加 和 "version"=null

版本

最佳答案

Spring-data也有自己的版本注释:org.springframework.data.annotation.Version。该注解在spring-data-mybatis lib中使用。

关于java - 由于破坏查询的条件,来自 org.springframework.data.mybatis.annotations.Version 的 @Version 在更新时不会增加 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61142951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com