简体   繁体   English

更新时,未在返回的实体上设置标记为updatable = false的spring数据审核字段

[英]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() . 当我创建实体的createdBycreatedDatelastModifiedBylastModifiedDate得到返回的对象上设置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()来更新现有的实体返回的对象不具有createdBycreatedDate集。

 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()更新实体我也得到一个对象回来createdBycreatedDate设置为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.

相关问题 保存带有标记为可更新 = 假的字段的实体时,禁用警告“实体已修改,但不会更新,因为该属性是不可变的” - Disable warning “entity was modified, but it won't be updated because the property is immutable” when saving entity with fields marked updatable=false 保留实体时,如何从不可更新字段的数据库中返回值? - How to returned the value from DB of non-updatable fields when persisting an entity? 我不希望Spring Data JPA更新实体时 - Spring Data JPA updates entity when I don't want it to 使用 Spring Data Auditing 在不覆盖 @Created 字段的情况下更新 Couchbase 文档 - Update Couchbase documents without overwriting @Created fields with Spring Data Auditing 休眠 @OneToOne with updatable = false 仍在更新 - Hibernate @OneToOne with updatable = false still updating 使用 Spring/hibernate 审计实体 - Auditing an entity using Spring/hibernate Spring + Hibernate审核(无Spring数据) - Spring + Hibernate auditing (no Spring Data) Spring Data MongoDB 审计不适用于嵌入式文档 - Spring Data MongoDB auditing doesn't work for embedded documents 批量插入时spring数据mongodb审计不起作用 - spring data mongodb auditing not work when bulk insert 在Spring Boot中未为同一对象设置标有@Transient批注的字段 - Fields marked with @Transient annotation are not being set for the same object in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM