英文:
Unable to get lastModifiedBy column in saved entity post saving in DB
问题
我已经创建了一个JPA实体,如下所示:
MyEntity 如下:
@Entity
@Table(name = "my_table")
public class MyEntity extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid",
strategy = "generator.CustomUUIDGenerator")
private String id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
}
AbstractAuditingEntity 如下:
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
@JsonIgnore
private String createdBy;
@CreatedDate
@Column(name = "created_date", updatable = false)
@JsonIgnore
private Instant createdDate = Instant.now();
@LastModifiedBy
@Column(name = "last_modified_by", length = 50)
@JsonIgnore
private String lastModifiedBy;
@LastModifiedDate
@Column(name = "last_modified_date")
@JsonIgnore
private Instant lastModifiedDate = Instant.now();
}
现在,我正在使用以下语句在数据库中更新实体:
MyEntity savedEntity = threatCanvasRepository.save(myEntity);
在 savedEntity
对象中,lastModifiedBy
设置为 null。
尽管如此,当我从数据库中检索相同的实体时,我能够看到 lastModifiedBy
已被设置。我无法理解我在这里漏掉了什么。
编辑
最奇怪的部分是,响应中我得到了 createdDate
和 lastModifiedDate
,但没有得到 createdBy
和 lastModifiedBy
。
英文:
I have created one JPA entity as follows:
MyEntity is as follows:
@Entity
@Table(name = "my_table")
public class MyEntity extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid",
strategy = "generator.CustomUUIDGenerator")
private String id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
AbstractAuditingEntity is as follows:
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@CreatedBy
@Column(name = "created_by", nullable = false, length = 50, updatable = false)
@JsonIgnore
private String createdBy;
@CreatedDate
@Column(name = "created_date", updatable = false)
@JsonIgnore
private Instant createdDate = Instant.now();
@LastModifiedBy
@Column(name = "last_modified_by", length = 50)
@JsonIgnore
private String lastModifiedBy;
@LastModifiedDate
@Column(name = "last_modified_date")
@JsonIgnore
private Instant lastModifiedDate = Instant.now();
Now, I am updating the entity in DB with following statement:
MyEntity savedEntity = threatCanvasRepository.save(myEntity);
Here, in the savedEntity
object, lastModifiedBy
is setting as null.
Even though, while I am fetching the same from DB, I am able to see that lastModifiedBy is set. I cannot understand what I am missing here.
EDIT
The weirdest part is, I am getting createdDate and lastModifiedDate in the response but not createdBy and lastModifiedBy.
答案1
得分: 0
我假设您的服务是 @Transactional
的,那么 Repository.save()
实际上不会立即将其保存到数据库,它会将其添加到当前事务以供以后处理。@CreatedDate
、@CreatedBy
、@LastModifiedBy
和 @LastModifiedDate
注解会在事务由 JPA(我假设是 Hibernate)实际提交时进行处理。因此,您需要在提交当前事务后从另一个服务中获取。但通常情况下,您实际上并不需要这样做。只要相信它们已经在数据库中了。
英文:
I assume that your service is @Transactional
, then Repository.save()
actually doesn't save it to DB immediately, it adds it to the current transaction for later processing. @CreatedDate
, @CreatedBy
, @LastModifiedBy
, and @LastModifiedDate
annotations are processed when the transaction is actually committing by JPA (I assume Hibernate). So, you need to fetch from another service after committing the current transaction. But usually, you just don't need it. Just believe that they are in DB.
答案2
得分: 0
以下是翻译好的内容:
你是否在你的应用程序中启用了 JPA 审计?
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
/*
如果你正在使用 Spring Security,你可以使用以下代码段来获取当前登录的用户名。
SecurityContextHolder.getContext().getAuthentication().getName()
*/
return new UsernameAuditorAware();
}
}
这是为了使 @LastModifiedBy
注解起作用所必需的。
英文:
Have you enabled JPA Auditing in your application?
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {
@Bean
public AuditorAware<String> auditorProvider() {
/*
if you are using spring security, you can get the currently logged username with following code segment.
SecurityContextHolder.getContext().getAuthentication().getName()
*/
return new UsernameAuditorAware();
}
}
This is required for @LastModifiedBy annotation to work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论