Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @Spring引导中未调用JPA实体的后构造_Java_Spring_Jpa_Spring Boot_Postconstruct - Fatal编程技术网

Java @Spring引导中未调用JPA实体的后构造

Java @Spring引导中未调用JPA实体的后构造,java,spring,jpa,spring-boot,postconstruct,Java,Spring,Jpa,Spring Boot,Postconstruct,我使用的是Spring Boot,我向我的JPA实体添加了@PostConstrcut注释,如图所示,但是当实体被实例化时,这个拦截器永远不会被调用 @Entity public class MyTableName implements java.io.Serializable { // all attributes @PostConstruct public void init(){ // This code never called System.out

我使用的是Spring Boot,我向我的JPA实体添加了@PostConstrcut注释,如图所示,但是当实体被实例化时,这个拦截器永远不会被调用

@Entity
public class MyTableName implements java.io.Serializable {
   // all attributes
   @PostConstruct
   public void init(){
     // This code never called
     System.out.println("PostConstruct");
   }
}

为什么要在实体bean中有@PostConstruct?我觉得这里有设计的味道。可能您指的是带有@PrePersist、@PreUpdate或@PostPersist@PostUpdate注释的方法,这些注释在保存实体之前或之后运行代码?

为什么您希望在实体bean中有@PostConstruct?我觉得这里有设计的味道。可能您指的是带有@PrePersist、@PreUpdate或@PostPersist@PostUpdate注释的方法,这些注释在保存实体之前或之后运行代码?

JPA实体不是spring管理的bean,因此spring永远不会调用
@PostConstruct
。实体有自己的实体侦听器注释,但没有任何与
@PostConstruct
语义相关的注释(
@PrePersist
@PostLoad
可能是最接近的)。实体需要有一个默认构造函数,因为所有JPA实现都使用Class.newInstance()作为默认实例化策略。一个好的JPA提供程序将允许您自定义实例化策略,因此您可以编写自己的
@PostConstruct
截取并调用它,如果需要,还可以
@Autowire
将Springbean转换为实体。但是,永远不要将JPA实体注册为SpringBean。

JPA实体不是Spring管理的Bean,因此Spring永远不会调用
@PostConstruct
。实体有自己的实体侦听器注释,但没有任何与
@PostConstruct
语义相关的注释(
@PrePersist
@PostLoad
可能是最接近的)。实体需要有一个默认构造函数,因为所有JPA实现都使用Class.newInstance()作为默认实例化策略。一个好的JPA提供程序将允许您自定义实例化策略,因此您可以编写自己的
@PostConstruct
截取并调用它,如果需要,还可以
@Autowire
将Springbean转换为实体。但是,永远不要将JPA实体注册为SpringBean。

我找到了解决方案,事实上,我没有使用@PostConstruct注释(由容器管理),而是使用@PostLoad(由ORM管理)。谢谢。

我找到了解决方案,事实上,我没有使用@PostConstruct注释(由容器管理),而是使用@PostLoad(由ORM管理)。谢谢。

我没有显示该类中的所有代码,但该实体具有其他具有@Transient annotation的属性(我不会持久化的属性),我想在实例化该实体后向这些属性添加值。我没有显示该类中的所有代码,但是这个实体还有其他属性具有@Transient annotation(我不会持久化的属性),我想在这个实体实例化之后为这些属性添加值。您应该检查给出解决方案的答案是否正确。如果您已经有了正确答案,请避免自动回答。您应该检查给出解决方案的答案是否正确。如果您已经有了正确答案,请避免自动应答。