我一直在使用spring数据休息没有任何问题,但现在我要求当用户对给定实体执行DELETE操作时,即DELETE /accounts/<id>我需要在数据库上设置一个标记,将该实体标记为已删除,但我确实要保留记录.
基本上这意味着我需要在数据库中执行UPDATE而不是DELETE操作.我没有找到任何方法来覆盖删除(ID)方法的弹簧行为.
一些代码:
@Entity
@Table(name = "account")
public class Account {
/*
Default value for this field is false but when a receive a
DELETE request for this entity i want to turn this flag
to false instead of deleting the record.
*/
@Column(name = "deleted")
private boolean deleted;
...
}
Run Code Online (Sandbox Code Playgroud)
帐户存储库
@RepositoryRestResource
public interface AccountRepository extends JpaRepository<Account, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我需要在每个日志行上记录一个常量值,但值的问题在于获取它的唯一方法是使用代码(我需要首先调用api来获取值).所以我想在应用程序的开头找出该值,然后在主线程中设置它,然后每个新线程继承该MDC上下文并且已经设置了该值.
目前我正在使用spring-boot,所以我尝试使用ApplicationListener进行设置:
@Bean
public ApplicationListener<ContextRefreshedEvent> setLoggingContext() {
return new ApplicationListener<ContextRefreshedEvent>() {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
MDC.put("instanceId", "THE-VALUE");
LOGGER.debug("Instance id set");
}
};
}
Run Code Online (Sandbox Code Playgroud)
该代码生成此日志:
12:18:59.096 [localhost-startStop-1] default INFO o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
12:18:59.171 [localhost-startStop-1] default THE-VALUE DEBUG c.m.config.CommonWebConfiguration - Instance id set
12:18:59.181 [localhost-startStop-1] default THE-VALUE INFO o.s.boot.SpringApplication - Started application in 5.917 seconds (JVM running for 15.429)
12:18:59.193 [main] default INFO o.a.coyote.http11.Http11Protocol - Starting ProtocolHandler ["http-bio-8081"]
12:18:59.205 [main] default INFO org.apache.coyote.ajp.AjpProtocol - …Run Code Online (Sandbox Code Playgroud)