2017-08-29 80 views
0

我有一個具有多個字段和2個瞬態字段的原型實體bean。其中之一是原型,其他則是單身。休眠 - 在加載時填充自動裁剪字段

@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 

} 

一切工作,直到我試圖從數據庫中讀取實例,因爲當Hibernate創建實例時,Spring不會自動裝載任何字段。

一些額外的信息:我使用CrudRepository和休眠由spring-boot自動配置。

我該如何妥善解決這種情況?這是預期的行爲,還是我應該爲hibernate使用spring上下文做一些額外的配置?

回答

0

我相信,你的問題是一樣的這樣一個問題:

Bean injection inside a JPA @Entity

看一看這一點。我相信,這會有所幫助。

+0

這似乎是唯一的方法,至少只有一個我發現。但是我認爲如果我將所有的邏輯合理分解爲服務並使實體pojos成爲更清晰的解決方案。說實話,我確信應該有一些方法讓hibernate從上下文獲取bean。無論如何,謝謝,我會再等一等,也許有人知道不同的解決方案。 –

+0

是的,絕對。 – gschambial