为什么 Spring Data JPA 没有设置 version 属性? [英] Why is the version property not set with Spring Data JPA?

查看:23
本文介绍了为什么 Spring Data JPA 没有设置 version 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Wanted to know how is @Version annotation in Spring Data REST put to use for ETags, I do not see the ETags populated for some reason

@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 值吗?如库中的片段所示

Going by the docs this should give me an Etag Value? as seen in the snippet from the library

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);

然而,给定实体 &以下配置,我仍然得到一个空的版本.我的应用程序有以下内容

however, given the entity & following configuration, I still get a null for Version. My Application has the following

@SpringBootApplication
@EnableEntityLinks
@EnableJpaAuditing
public class GabbarSinghApplication

Rest Repository 如下

And the Rest Repository is as follows

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

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

While I haven't got to test these methods yet with the headers etc, a simple POST request on http://localhost:8080/workshops gives a 500 because of null pointer exception at getting ETag header value from value of version property.

更新

实体转移到@javax.persistence.Version,我仍然没有在响应头中得到一个 ETag 头.

Moved to @javax.persistence.Version for the entities, I still do not get an ETag header in the response headers.

这是一个失败的单元测试

Here's a failing unit test

  @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());

此断言失败.

推荐答案

我遇到了同样的问题,经过几个小时和几个小时后,我意识到以下事情让我有所启发 =P

I faced the same problem and after hours and hours I realized the following things that led me to the enlightenment =P

  1. Spring Data Rest 仅在 2.3.0 版本之后为乐观并发控制提供 ETag 支持.请参阅大约一年前发布的此错误.以前版本的 Spring Data Rest 不会填充 ETag 标头.
  2. 对于 Spring Boot 应用程序(我们的案例),您需要使用 Spring Boot 1.3.0.BUILD-SNAPSHOT 或更高版本才能设置依赖 Spring Data Rest 的 spring-boot-starter-data-rest2.4.1(高于 2.3.1,这正是我们需要的:P).
  1. Spring Data Rest provides ETag support for optimistic concurrency control ONLY after version 2.3.0. See this bug published about a year ago. Previous versions of Spring Data Rest will NOT populate the ETag header.
  2. For Spring Boot applications (our case), you need to use Spring Boot 1.3.0.BUILD-SNAPSHOT or higher in order to be able to setup a spring-boot-starter-data-rest that depends on Spring Data Rest 2.4.1 (higher than 2.3.1 which is exactly what we need :P).

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

In my case I was using Spring Boot 1.2.7 and whenever I installed the spring-boot-starter-data-rest dependency I ended up getting the Spring Data Rest 2.2.0 which don't has the ETag support. After upgrading Spring Boot in my project and reinstalled the dependencies my REST API started to retrieve the ETag header =D

这篇关于为什么 Spring Data JPA 没有设置 version 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆