为什么没有使用Spring Data JPA设置版本属性? [英] Why is the version property not set with Spring Data JPA?
问题描述
想知道如何在用于ETag的Spring Data REST中使用 @Version
注释,我没有看到ETag由于某种原因填充
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);
但是,鉴于实体&在配置之后,我仍然为版本获取null。
我的申请表包含以下内容
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库如下
@RepositoryRestResource(collectionResourceRel = "venue", path = "venues")
public interface VenueRepository extends JpaRepository<Venue, Long> {
虽然我还没有用标题等测试这些方法,但是一个简单的POST请求在 http:// localhost:8080 / workshops
给出 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。对于实体的版本,我仍然没有在响应头中获得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());
此断言失败。
推荐答案
使用Spring Data JPA,您需要使用 @ javax.persistence.Version
。 @ org.springframework.data.annotation.Version
是用于其他Spring Data模块的注释。
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.
这篇关于为什么没有使用Spring Data JPA设置版本属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!