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:14:27 EST