0

I have a model A and B and a relation model A_B

Now I want to have a getter in class A get something from class A-B So I @Autowired the repo of A_B in the model A but it gives an error.

Is there any way to use a postconstruct kinda Autowire?

Here is my code:

@autowire IrepoA_B irepoA_B;

@PostConstruct
public boolean getVar() {
    return irepoA_B.getByTrackId(this.getId()).var();
}

How do I fix this so that he does not give the error:

Error creating bean with name 'entityManagerFactory' defined in class

path resource........

Community
  • 1
  • 1
Nick Alexander
  • 361
  • 2
  • 5
  • 21
  • Why `@PostConstruct` on a getter? It should be a `void` method. This might (and probably) will lead to too early querying of the database (before everything has been setup) and give you errors). – M. Deinum Nov 05 '18 at 07:57
  • I hoped it would't init the autowired till it got used so according to my (flawed) logic it would only init the repo after starting the application – Nick Alexander Nov 05 '18 at 11:01
  • No... The `@PostConstruct` is called as soon as the object is created and dependencies have been satisfied. This could be even before a transactional proxy etc. have been created. – M. Deinum Nov 05 '18 at 11:04

1 Answers1

0

Well the problem was in a whole different corner than I thought.

The problem was that it tried to make a column irepoA_B because it was used in an @Entity so I used the annotation @Transient. That fixed the error

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nick Alexander
  • 361
  • 2
  • 5
  • 21