0

I've got an entity class and few services. That works well. But now I want to use a service inside an entity. I've added @Configurable and @Transient annotation to entity class and also <context:spring-configured /> statement to root-context.xml. Console says:

java.lang.NullPointerException

I've checked if service field is null and yes, it is. What is wrong?

Tony
  • 3,605
  • 14
  • 52
  • 84

1 Answers1

1

Entities are not Spring beans. Spring doesn't know about them, and is not responsible for their instantiation. It thus can't inject anything into them.

Calling a service from an entity is just not something you should do. The service layer uses the domain layer, but not vice-versa.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • But that post says it's possible http://stackoverflow.com/questions/5594934/hibernate-entity-autowire – Tony Feb 02 '15 at 11:35
  • And it explains how to do it. So, if you really want that (which, I repeat, is not something you should do), then follow the answer: as it says, you need to enable aspectJ instrumentation of your byte-code. – JB Nizet Feb 02 '15 at 11:38