Re: Cannot override setPersistenceState in 3.0-M5

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Jan 21 2009 - 07:23:01 EST

  • Next message: Bob Schellink: "Re: Cannot override setPersistenceState in 3.0-M5"

    Yeah, setPersistentState(..) was used as a simple way to intercept
    lifecycle events before listeners got introduced in 3.0. Now with
    listeners in the picture, certain things have changed, and
    'setPersistentState' no longer works as before. (Hmm... maybe for
    consistency we should hide this method from the public interface of
    the object?)

    > BTW I think we need better exception handling in such situation
    > (e.g. throw
    > CayenneRuntimeException("Object not registered"), not just NPE).

    +1.

    Andrus

    On Jan 21, 2009, at 3:13 PM, Andrey Razumovsky wrote:

    > Hi Bob,
    >
    > The exception occurs because object is not yet registered when you
    > change
    > its properties. I think the best way to set default values for
    > object is via
    > LifecycleListeners. Did this work prior to M5?
    > BTW I think we need better exception handling in such situation
    > (e.g. throw
    > CayenneRuntimeException("Object not registered"), not just NPE). I
    > even
    > think I've seen some JIRA about it
    >
    > Andrey
    >
    > 2009/1/21 Bob Schellink <sabob..mail.com>
    >
    >> Hi all,
    >>
    >> Perhaps this is a known issue but there seems to be a slight
    >> regression in 3.0-M5 when overriding setPersistenceState.
    >> When invoking dataContext#newObject a NPE is thrown.
    >>
    >> The reason I want to override setPersistenceState is to set the
    >> inheritance type of the hierarchy. For example:
    >>
    >> public class Manager extends _Manager {
    >>
    >> public void setPersistenceState(int state) {
    >> super.setPersistenceState(state);
    >>
    >> if(state == PersistenceState.NEW) {
    >> setEmployeeType("MANAGER");
    >> }
    >> }
    >> }
    >>
    >>
    >> To test I use:
    >>
    >> public static void main(String[] args) {
    >> DataContext dc = DataContext.createDataContext();
    >>
    >> Employee emp = (Employee) dc.newObject(Manager.class); // <-- this
    >> call fails
    >> dc.commitChanges();
    >> }
    >>
    >>
    >> Stacktrace:
    >>
    >> Exception in thread "main" java.lang.NullPointerException
    >> at
    >> org.apache.cayenne.access.ObjectStore.registerDiff(ObjectStore.java:
    >> 187)
    >> at
    >> org
    >> .apache
    >> .cayenne.access.ObjectStore.nodePropertyChanged(ObjectStore.java:
    >> 1080)
    >> at
    >> org
    >> .apache
    >> .cayenne
    >> .util
    >> .ObjectContextGraphAction
    >> .handleSimplePropertyChange(ObjectContextGraphAction.java:83)
    >> at
    >> org
    >> .apache
    >> .cayenne
    >> .util
    >> .ObjectContextGraphAction
    >> .handlePropertyChange(ObjectContextGraphAction.java:67)
    >> at
    >> org
    >> .apache.cayenne.access.DataContext.propertyChanged(DataContext.java:
    >> 1591)
    >> at
    >> org
    >> .apache
    >> .cayenne.CayenneDataObject.writeProperty(CayenneDataObject.java:226)
    >> at test.auto._Employee.setEmployeeType(_Employee.java:16)
    >> at test.Manager.setPersistenceState(Manager.java:13)
    >> at
    >> org.apache.cayenne.access.DataContext.newObject(DataContext.java:750)
    >> at
    >> org.apache.cayenne.access.DataContext.newObject(DataContext.java:715)
    >> at test.Test.main(Test.java:17)
    >>
    >>
    >> Should I open a JIRA for this?
    >>
    >> kind regards
    >>
    >> bob
    >>



    This archive was generated by hypermail 2.0.0 : Wed Jan 21 2009 - 07:23:35 EST