描述
在Spring 1.5.9应用程序中,
我在@Entity on version属性中使用version注释来获得每次更新时递增的版本,但它总是破坏我的应用程序。
错误是:
update effect 0 row, maybe version control lock occurred.我可以看到,请求的结尾是:
"version"="version"+1,
where
"id"=21
and "version"=null and "version"=null导致整个请求编辑0 row。
这是实体的一个例子:
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> {
}这是同一个实体的服务接口:
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列在每次更新时都会增加。
结果
相反,每个更新都为version列保留一个null值。
问题
spring-data-mybatis在请求结束时追加and "version"=null?版本
发布于 2020-04-10 19:38:17
Spring也有自己的版本注释:org.springframework.data.annotation.Version。此注释用于中。
https://stackoverflow.com/questions/61142951
复制相似问题