@OneToMany property null in Entity after (second) merge
I'm using JPA (with Hibernate) and Gilead in a GWT project. On the server side I have this method and I'm calling this method twice with the same "campaign". On the second call it throws a null pointer exception in line 4 "campaign.getTextAds()"
public List<WrapperTextAd> getTextAds(WrapperCampaign campaign) {
campaign = em.merge(campaign);
System.out.println("getting textads for "+campaign.getName());
for(WrapperTextAd textad: campaign.getTextAds()) {
//do nothing
}
return开发者_StackOverflow new ArrayList<WrapperTextAd>(campaign.getTextAds());
}
The code in WrapperCampaign Entity looks like this
@OneToMany(mappedBy="campaign")
public Set<WrapperTextAd> getTextAds() {
return this.textads;
}
Since line 3 doesn't blow, campaign is not null. The for each loop will throw NPE if the collection to be iterated is null (as will trying to initialize a new ArrayList with a null collection parameter). You should guard against this:
if(campaign.getTextAds() != null) {
for(WrapperTextAd textad: campaign.getTextAds()) {
//do nothing
}
}
精彩评论