[英]spring data auditing fields marked as updatable=false don't get set on the returned entity when updating
I'm using Spring Data's annotations to add auditing data to my entities when they are saved or updated. 我使用Spring Data的注释在保存或更新实体时将审计数据添加到我的实体中。 When I create the entity the
createdBy
, createdDate
, lastModifiedBy
and lastModifiedDate
get set on the object returned by repository.save()
. 当我创建实体的
createdBy
, createdDate
, lastModifiedBy
和lastModifiedDate
得到返回的对象上设置repository.save()
。
ResourceEntity(id=ebbe1f3d-3359-4295-8c83-63eab21c4753, createdDate=2018-09-07T21:11:25.797, lastModifiedDate=2018-09-07T21:11:25.797, createdBy=5855070b-866f-4bc4-a18f-26b54f896a4b, lastModifiedBy=5855070b-866f-4bc4-a18f-26b54f896a4b)
Unfortunately, when I call repository.save()
to update an existing entity the object returned does not have the createdBy
and createdDate
set. 不幸的是,当我打电话
repository.save()
来更新现有的实体返回的对象不具有createdBy
和createdDate
集。
ResourceEntity(id=ebbe1f3d-3359-4295-8c83-63eab21c4753, createdDate=null, lastModifiedDate=2018-09-07T21:12:01.953, createdBy=null, lastModifiedBy=5855070b-866f-4bc4-a18f-26b54f896a4b)
All the fields are set correctly in the database and a call to repository.findOne()
outside of my service class returns an object with all the fields set correctly. 在数据库中正确设置了所有字段,并且对服务类外部的
repository.findOne()
的调用返回了一个具有正确设置所有字段的对象。
ResourceEntity(id=ebbe1f3d-3359-4295-8c83-63eab21c4753, createdDate=2018-09-07T21:11:25.797, lastModifiedDate=2018-09-07T21:12:01.953, createdBy=5855070b-866f-4bc4-a18f-26b54f896a4b, lastModifiedBy=5855070b-866f-4bc4-a18f-26b54f896a4b)
But if I call repository.findOne()
in the service right after calling repository.save()
to update the entity I also get an object back with createdBy
and createdDate
set to null. 但是,如果我叫
repository.findOne()
调用之后在服务repository.save()
更新实体我也得到一个对象回来createdBy
和createdDate
设置为null。
Here is my entity: 这是我的实体:
@Entity(name = "resource")
@EntityListeners(AuditingEntityListener.class)
@Table(name = "resource")
@Data
@EqualsAndHashCode(of = "id")
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ResourceEntity {
@Id
@org.hibernate.annotations.Type(type = "org.hibernate.type.PostgresUUIDType")
private UUID id;
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@CreatedBy
@Column(nullable = false, updatable = false)
@org.hibernate.annotations.Type(type = "org.hibernate.type.PostgresUUIDType")
private UUID createdBy;
@LastModifiedBy
@org.hibernate.annotations.Type(type = "org.hibernate.type.PostgresUUIDType")
private UUID lastModifiedBy;
}
Here is my service: 这是我的服务:
@Component
public class ResourceService {
@Autowired
private ResourceRepository resourceRepository;
public ResourceEntity createResource(ResourceEntity resourceEntity) {
return saveResource(resourceEntity);
}
public ResourceEntity updateResource(ResourceEntity resourceEntity) {
return saveResource(resourceEntity);
}
public ResourceEntity getResource(UUID resourceId) {
return resourceRepository.findOne(resourceId);
}
private ResourceEntity saveResource(ResourceEntity resourceEntity) {
ResourceEntity savedResourceEntity = resourceRepository.save(resourceEntity);
return savedResourceEntity;
}
}
Here is my test: 这是我的测试:
def "Test update"() {
given:
UUID id = aRandom.uuid()
Resource resource = aRandom.resource().id(id).build()
Resource savedResource = resourceClient.createResource(resource)
when:
Resource updatedResource = aRandom.resource().id(id).build()
updatedResource = resourceClient.updateResource(updatedResource)
then:
Resource result = resourceClient.getResource(id)
assert result.id == updatedResource.id
assert result.createdBy == updatedResource.createdBy
assert result.creationDate == updatedResource.creationDate
assert result.lastModifiedBy == updatedResource.lastModifiedBy
assert result.lastModifiedDate == updatedResource.lastModifiedDate
}
I tried before like this when i need to add audit info. 我需要添加审核信息时就这样尝试过。
I have a DataConfig
class like this. 我有一个这样的
DataConfig
类。
@Configuration
@EnableJpaAuditing
public class DataConfig {
@Bean
public AuditorAware<UUID> auditorProvider() {
return new YourSecurityAuditAware();
}
@Bean
public DateTimeProvider dateTimeProvider() {
return () -> Optional.of(Instant.from(ZonedDateTime.now()));
}
}
Now you need AuditorAware
class to get audit info. 现在,您需要
AuditorAware
类来获取审核信息。 So this class will be like this ; 所以这个班级将会是这样;
public class XPorterSecurityAuditAware implements AuditorAware<UUID> {
@Override
public Optional<UUID> getCurrentAuditor() {
//you can change UUID format as well
return Optional.of(UUID.randomUUID());
}
}
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.