users@glassfish.java.net

_at_OneToMany includes null value in second call

From: <glassfish_at_javadesktop.org>
Date: Wed, 02 Jun 2010 05:25:52 PDT

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<ItemImage> images;

    public Long getId() {
        return id;
    }

    public List<ItemImage> 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?
[Message sent by forum member 'mkemal']

http://forums.java.net/jive/thread.jspa?messageID=472388