【问题标题】:Why is the version property not set with Spring Data JPA?为什么没有使用 Spring Data JPA 设置版本属性?
【发布时间】:2015-10-31 03:52:06
【问题描述】:

想知道 Spring Data REST 中的 @Version 注释如何用于 ETag,但由于某种原因我没有看到填充的 ETag

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Venue implements Serializable {

  private static final long serialVersionUID = -5516160437873476233L;

  private Long id;

  ...
  // other properties

  private Long version;

  private Date lastModifiedDate;

  // getters & setters

  @JsonIgnore
  @LastModifiedDate
  public Date getLastModifiedDate() {
    return lastModifiedDate;
  }

  @Version
  @Column
  public Long getVersion() {
    return version;
  }

按照文档,这应该会给我一个 Etag 值吗?如图书馆的sn-p所示

protected HttpHeaders prepareHeaders(PersistentEntity<?, ?> entity, Object value) {

    // Add ETag
    HttpHeaders headers = ETag.from(entity, value).addTo(new HttpHeaders());

    // Add Last-Modified
    AuditableBeanWrapper wrapper = getAuditableBeanWrapper(value);

但是,鉴于实体和以下配置,我仍然得到版本为空。 我的应用程序有以下

@SpringBootApplication
@EnableEntityLinks
@EnableJpaAuditing
public class GabbarSinghApplication

而Rest Repository如下

@RepositoryRestResource(collectionResourceRel = "venue", path = "venues")
public interface VenueRepository extends JpaRepository<Venue, Long> {

虽然我还没有使用标头等测试这些方法,但在 http://localhost:8080/workshops 上的简单 POST 请求会给出 500,因为从版本属性值获取 ETag 标头值时出现空指针异常。

更新

实体已移至 @javax.persistence.Version,但我仍然没有在响应标头中获得 ETag 标头。

这是一个失败的单元测试

  @Before
  public void setUp() throws Exception {

    XStream xstream = new XStream();
    ObjectInputStream in = xstream.createObjectInputStream(venuesXml.getInputStream());
    leela = (Venue) in.readObject();
    paul = (Venue) in.readObject();
    taj = (Venue) in.readObject();
    LOGGER.debug("Initialised Venues from xml file {}", venuesXml.getFilename());

  }

  @Test
  public void testEtagHeaderIsAutoGeneratedOnResourceCreation() {

    final HttpEntity<Venue> httpEntity = new HttpEntity<Venue>(taj, headers);

    ResponseEntity<ResourceSupport> response = restTemplate.exchange(BASE_LOCATION
        + VENUES_ENDPOINT, HttpMethod.POST, httpEntity,
        new ParameterizedTypeReference<ResourceSupport>() {
        });

    assertTrue("Response should contain ETag header", null != response.getHeaders().getETag());

此断言失败。

【问题讨论】:

  • 我知道这已经有一段时间了,但我也面临着同样的问题......你明白了吗?

标签: java spring spring-data spring-data-jpa spring-data-rest


【解决方案1】:

使用 Spring Data JPA,您需要使用@javax.persistence.Version。 @org.springframework.data.annotation.Version 是用于其他 Spring Data 模块的注解。

【讨论】:

【解决方案2】:

我遇到了同样的问题,经过几个小时后,我意识到以下导致我启蒙的事情=P

  1. Spring Data Rest 仅在 2.3.0 版之后才为乐观并发控制提供 ETag 支持。见this bug published about a year ago。以前版本的 Spring Data Rest 不会填充 ETag 标头。
  2. 对于 Spring Boot 应用程序(我们的案例),您需要使用 Spring Boot 1.3.0.BUILD-SNAPSHOT 或更高版本才能设置依赖于 Spring Data Rest 的 spring-boot-starter-data-rest 2.4.1(高于 2.3.1,这正是我们所需要的:P)。

在我的情况下,我使用的是 Spring Boot 1.2.7,每当我安装 spring-boot-starter-data-rest 依赖项时,我最终都会得到不支持 ETag 的 Spring Data Rest 2.2.0。在我的项目中升级 Spring Boot 并重新安装依赖项后,我的 REST API 开始检索 ETag 标头 =D

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2021-12-08
    相关资源
    最近更新 更多