Skip to main content

@OneToMany includes null value in second call

1 reply [Last post]
mkemal
Offline
Joined: 2009-11-14

I wish this is the correct forum and I am sorry for the obscure title. However, i am dealing with a weird problem that i could not find any suitable words to define.Here is my situation;

I am developing an enterprise application with Glassfish V3 using EclipseLink as persistence provider. In some point in my project, i have two entity (Item and ItemImage) and I have a stateful session bean (ItemServiceBean) operating on these entities with a extended persistence context. Here is the related code;

/***** Item Entity **/

@Entity(name="Item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="item_id")
protected Long id;

@OneToMany(mappedBy = "item")
@OrderBy("listOrder")
private List images;

public Long getId() {
return id;
}

public List getImages() {
return images;
}

....
}

/***** ~Item Entity **/

/***** ItemImage Entity **/

@Entity
public class ItemImage{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="item_image_id")
private Long id;

@ManyToOne
@JoinColumn(name="item_id", nullable=false)
private Item item;

public Long getId() {
return id;
}

public void setItem(Item item) {
this.item = item;
}

...

}

/***** ~ItemImage Entity **/

/***** ItemServiceBean **/

@Stateful
public class ItemServiceBean implements ItemService {
@PersistenceContext(unitName = "myProject-ejbPU", type=PersistenceContextType.EXTENDED)
private EntityManager em;

public Item getItem(long id) {
return em.find(Item.class, id);
}

public ItemImage addItemImage(long itemId, File image) {
Item item = em.find(Item.class, itemId);
ItemImage itemImage = null;

// check image and item
if (item != null && image ImageUtil.createItemImages(itemImage, image)) {
// if fails, remove the created image entity
em.remove(itemImage);
}
else {
// when success, first check if any image added before
if (item.getImages().size() == 0) {
// if this image is first one, set the default image
item.setDefaultImage(itemImage);
}

// add image to item's image list
item.getImages().add(itemImage);

// log image creation
logImageCreation(itemId, itemImage.getId());
}

}

// return image if success, null otherwise
return itemImage;

}
...

}

/***** ~ItemServiceBean **/

So, at some point i upload an image for an item with addItemImage business method. It create, process and adds the image to the related item's image list. After the method call i access the item's image list without any problem.

However, when i destroy this session bean and create a new one with a new persistence context things get weird. If I call the getItem method for the previous item, the returned item entity gives me an image list which has a null value instead of last added ItemImage entity. If i restart Glassfish, i can get the list without any problem. That is, the newly created ItemImage entity was succesfully persisted.

So, i think there is a problem with the caching structure.

Any ideas?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kwlooi
Offline
Joined: 2005-01-19

Hello,
I am just curious if you have gotten any response or resolution to this issue. I am running into the same issue using WLS 10.3.2. I am getting some null elements back in my subsequent transaction in my ArrayList collection. We have tracked the issue down to EclipseLink caching. I wonder if you call em.flush() in your main transaction before you call commit?
Thanks.
Looi