Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - Why is the version property not set with Spring Data JPA?

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

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

And the Rest Repository is as follows

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

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.

Update

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

This assertion fails.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With Spring Data JPA, you need to use @javax.persistence.Version. @org.springframework.data.annotation.Version is the annotation to use for other Spring Data modules.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...