Ich möchte Entity mit zusätzlichen Daten beim Laden von DB innerhalb @PostLoad
anreichern.Zugriff auf Spring-Beans in Hibernate verwalteter Entity-Methode, die mit @PostLoad markiert ist
Wie kann ich auf Spring verwaltete Bohnen innerhalb @PostLoad
Methode zugreifen?
Ich benutze hässliche Lösung mit statischem Accessor:
@Service
public class StaticApplicationContext implements ApplicationContextAware {
private static ApplicationContext ctx = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
}
@Entity
public class Car {
@Id
private Long id;
...
@Transient
private List<XType> details;
@PostLoad
private void onLoad() {
XTypeRepository repo = StaticApplicationContext.getCtx()
.getBean(XTypeRepository.class) ;
this.details = repo.findByCarId(this.id);
}
}
Entsprechende Idee für static
beschrieben Zugang in Accessing spring beans in static method
Gibt es mehr idiomatische Lösung/framework Zucker?