我使用spring 3.2,彈簧數據和jpa。延遲加載返回空值
我保存廣告對象, 後,我保存消息
我嘗試從廣告獲得的消息,但它是空
@Entity
public class Advertisement implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="id", cascade={CascadeType.REMOVE}, fetch=FetchType.LAZY)
private Set<Message> messages;
}
@Entity
public class Message implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Advertisement advertisement;
}
測試單元
Advertisement ads = new Advertisement();
ads = advertisementRepo.save(ads);
assertNotNull(ads);
assertNotNull(ads.getId());
Message message = new Message();
message.setAdvertisement(ads);
message = msgRepo.save(message);
ads = advertisementRepo.findOne(ads.getId());
ads.getMessages(); //return null
爲什麼ads.getMessages( )不要返回消息?
該工作,但在廣告中,我需要做的: 設置 messages = new HashSet (); 之前我有:設置消息; –
當然,你需要初始化集合,我忘了提及。 – zagyi