Muốn biết thế nào là @Version
chú thích trong mùa xuân dữ liệu REST của đưa vào sử dụng cho ETags, tôi không thấy ETags dân cư đối với một số lý doTại sao thuộc tính phiên bản không được đặt với JPA dữ liệu Spring?
@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;
}
Đi theo các tài liệu này sẽ cung cấp cho tôi một giá trị ETag? như đã thấy trong đoạn mã từ thư viện
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);
Tuy nhiên, do thực thể & cấu hình sau, tôi vẫn nhận được một null cho Version. Ứng dụng của tôi có như sau
@SpringBootApplication
@EnableEntityLinks
@EnableJpaAuditing
public class GabbarSinghApplication
và phần còn lại Repository là như sau
@RepositoryRestResource(collectionResourceRel = "venue", path = "venues")
public interface VenueRepository extends JpaRepository<Venue, Long> {
Trong khi tôi đã không có để thử nghiệm các phương pháp này nhưng với các tiêu đề vv, một yêu cầu POST đơn giản trên http://localhost:8080/workshops
cho a 500
vì ngoại lệ con trỏ null khi nhận giá trị tiêu đề ETag từ giá trị của thuộc tính phiên bản.
Cập nhật
Chuyển đến @ javax.persistence.Version cho các đối tượng, tôi vẫn không nhận được một ETag trong các tiêu đề phản ứng.
Dưới đây là một thử nghiệm đơn vị thất bại
@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());
khẳng định này không thành công.
Tôi biết đã lâu rồi, nhưng tôi đang đối mặt với cùng một vấn đề ... bạn đã tìm ra nó chưa? – Fred