Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
Accessing to Spring beans in Hibernate managed entity method marked by @PostLoad

I would like to enrich entity with additional data on load from DB inside @PostLoad.

How can I access to Spring managed beans inside @PostLoad method?

I use ugly solution with static 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);
    }
}

Corresponding idea for static access described in Accessing spring beans in static method

Is there more idiomatic solution / framework sugar?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@OneToMany
@JoinFormula(value = "SELECT * FROM xTypeTable xt WHERE xt.carId = id") 
private List<XType> pastArticles;

Can't you just use JoinFormula and load the data with standard tools? Just write SQL which retrieves data for the XType. In the example above replace id with column name of id property (if it's different)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!