0

I have a prototype entity bean with multiple fields and 2 transient ones. Of these one is prototype as well and other is singleton.

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Entity
@Table(name = "tableA")
class A{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    @Autowired
    @Transient
    private CacheA cache; //Prototype bean for caching some calc results

    @Autowired
    @Transient
    private ServiceA service; //Singleton service bean

}

Everything works until I try to read instance from database, because when Hibernate creates instance, Spring wont autowire any field.

Some additional info: I'm using CrudRepository and hibernate autoconfigured by spring-boot.

How do I properly solve such case? Is this intended behaviour, or should I make some additional configuration for hibernate to use spring context?

K. Kowalczyk
  • 967
  • 13
  • 34

1 Answers1

0

I believe, your problem is same as in this question:

Bean injection inside a JPA @Entity

Have a look at this. I am sure, it will help.

gschambial
  • 1,383
  • 2
  • 11
  • 22
  • It appears to be the only way, at least only one I found. But I think it will make for cleaner solution if I properly separate all logic into services and make entities pojos. To be honest I was sure that there should be some way to make hibernate get bean from context. Anyway, thanks, I'll wait some more though, maybe someone knows different solution. – K. Kowalczyk Aug 29 '17 at 09:27
  • Yes, definitely. – gschambial Aug 29 '17 at 10:15