2017-01-03 29 views
2

爲什麼Spring JPA不初始化LAZY屬性MyChildEntity.myParentEntity(所有字段爲空)?即使在事務中,Spring JPA也不初始化懶惰屬性

我試圖使用Hibernate.initialize@Transactional,但這沒有幫助。

我的服務:

@Service 
@Transactional 
public class MyService { 

    @Resource 
    private MyChildEntityRepository myChildEntityRepository; 

    @Resource 
    private MyParentEntityRepository myParentEntityRepository; 

    @PostConstruct 
    public void init() { 
     MyParentEntity p = myParentEntityRepository.save(new MyParentEntity("my name")); 

     myChildEntityRepository.save(new MyChildEntity(p, "first value")); 
     myChildEntityRepository.save(new MyChildEntity(new MyParentEntity(1L, "another name"), "another value")); 

     // At this point both MyChildEntity's are in database and have correct foreign key value 
     List<MyChildEntity> result = myChildEntityRepository.findAll(); 
     //even this doesn't help, myParentEntity property still has all fields equals to null 
     Hibernate.initialize(result.get(0).getMyParentEntity()); 

     MyParentEntity p2 = result.get(0).getMyParentEntity(); 

     //trigger proxy's method to initialize lazy field 
     System.out.print(p2.getName()); // null 
     System.out.println(p2.getId()); // null 

     // PROBLEM: p2 has all fields equals null 
     // the same for result.get(1) 

     // BUT, this works correct - returns (1L, "my name") entity 
     myParentEntityRepository.findAll(); 
    } 
} 

子實體:

@Entity 
public class MyChildEntity { 

    @Id 
    @SequenceGenerator(sequenceName = "CHILD_SEQ", name = "ChildSeq", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ChildSeq") 
    private Long id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "my_parent_entity_id", referencedColumnName = "id") 
    private MyParentEntity myParentEntity; 

    @Column 
    private String value; 

    // constructors, getters, setters... 

父實體:

@Entity 
public class MyParentEntity { 

    @Id 
    @SequenceGenerator(sequenceName = "WORKFLOW_SEQ", name = "WorkflowSeq", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "WorkflowSeq") 
    private Long id; 

    @Column 
    private String name; 

    //constructors, getters, setters... 
+0

您是否缺少@JoinColumn? – BhathiyaW

+0

@BhathiyaW我和\ @JoinColumn有同樣的問題(我更新了我的問題)。最後我意識到這是一個Intellij調試器的問題,我的不好 –

回答

0

fetch屬性表明當相關實體應該從 使用數據庫進行檢索javax.persistence.FetchType枚舉。 FetchType.EAGER表示JPA 提供程序必須在檢索實體時檢索值。另一方面,FetchType.LAZY 作爲JPA提供程序的提示,它只能在第一次訪問屬性 (可能永遠不會,從而節省到數據庫的旅程)時才能等待並獲取值。 但是,JPA提供商 不需要支持延遲加載,因此無論如何可以加載這些值。

來源:專業的Java Web應用程序通過尼古拉斯·威廉姆斯小號

編輯:

我真的很抱歉我把這個長。這是我認爲是錯誤的。我沒有在父實體中看到子實體的實例。它應該看起來像這樣:

public class MyParentEntity { 
... //other fields 
@OneToMany(fetch = FetchType.LAZY, mappedBy = "myParentEntity") 
private Set<MyChildEntity> myChildEntities = new HashSet<MyChildEntity>; 
... //other fields or constructors or getters or setters 
...  
} 

我希望這可以工作。如果不是,那麼在您的MyChildEntity課程中,@JoinColumn內部會有一個奇怪的註釋,稱爲referencedColumnName。我不知道那是什麼。請刪除它。

謝謝

+0

謝謝,但問題是我根本無法獲取懶惰屬性。意思是我無法通過Spring JPA初始化它的字段 –