dev@glassfish.java.net

_at_PostLoad behaviour

From: Chris Loftus <cwl_at_aber.ac.uk>
Date: Tue, 12 Aug 2008 11:55:47 +0100

Dear All,

I was surprised when an em.merge(bookItem) operation caused the
@PostLoad callback method in bookItem to run (I stepped through with the
NB debugger to check this). I always thought it was only called after a
find or getReference or co-related query. I am running TopLink, version:
Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))
on Glassfish V2ur2 (in debug mode).

The code in my JSF backing bean that triggers the callback:
public class BookItemController{
...
@Resource
private UserTransaction utx = null;
   
@PersistenceContext
private EntityManager em;

private BookItem bookItem;
....

public void edit(){
...
try {
            // What this section tries to do is merge the bookItem's
persistent data
            utx.begin();
                     
            // We now merge all the bookItem changes with the persistent
            // representation (make it managed)
            bookItem = em.merge(bookItem);
           
            utx.commit();
            addSuccessMessage(null, "bookItemSuccessfullyUpdated", null);
        } catch (Exception ex) {
...
}
}

And the BookItem callbacks (including PostLoad):

 @PrePersist
    @PreUpdate
    private void authorsArrayToString(){
        StringBuffer result = new StringBuffer();
        result = authorsArrayToString(result);
        authorsStr = result.toString();
    }
   
    @PostLoad
    private void authorsStringToArray(){
        authors = authorsStr.split(":");
    }
   
    private StringBuffer authorsArrayToString(StringBuffer currentStr){
        if (authors != null){
            for (int i=0; i<authors.length; i++){
                currentStr.append(authors[i]);
                if (i < authors.length-1)
                    currentStr.append(':');
            }
        }
        return currentStr;
    }


Looking forward to you response

Chris

-- 
Chris Loftus mbcs,	     | PHONE: +44 (0)1970 622422
Computer Science Department, | FAX  : +44 (0)1970 628536
Aberystwyth University,      | Email: cwl_at_aber.ac.uk
Aberystwyth, SY23 3DB, UK    | WWW: http://www.aber.ac.uk/compsci