Submitting values for null associations

2014-02-11 Thread ChambreNoire
Hi,

I have a form which can flip between edit and view modes. Its fields share a
common CompoundPropertyModel which in turn points to an AbstractEntityModel
similar to that described in the 'Wicket in Action' blog. 

My problem is with associations that happen to be null. If I have a User
entity with a lazily initialised Job association, during viewing all works
fine but when I edit the user and enter some text into a field that is bound
to User.Job.Name I get a WicketRuntimeException: Attempted to set property
value on a null object. How is this kind of situation generally handled?

Many thanks,

Chambre

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Submitting values for null associations

2014-02-11 Thread francois meillet
The entity User needs to have a Job's instance when you construct your
model.

François


On Tue, Feb 11, 2014 at 2:56 PM, ChambreNoire a...@tentelemed.com wrote:

 Hi,

 I have a form which can flip between edit and view modes. Its fields share
 a
 common CompoundPropertyModel which in turn points to an AbstractEntityModel
 similar to that described in the 'Wicket in Action' blog.

 My problem is with associations that happen to be null. If I have a User
 entity with a lazily initialised Job association, during viewing all works
 fine but when I edit the user and enter some text into a field that is
 bound
 to User.Job.Name I get a WicketRuntimeException: Attempted to set
 property
 value on a null object. How is this kind of situation generally handled?

 Many thanks,

 Chambre

 --
 View this message in context:
 http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365.html
 Sent from the Users forum mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org




Re: Submitting values for null associations

2014-02-11 Thread ChambreNoire
Yes I'm aware of this but the problem is I'm reusing the same
AbstractEntityModel (effectively a LoadableDetachableModel that loads from
the db) for the edit mode and the User instance in the db has no Job
instance (yet). I could always re-build a Model using the unproxied User
instance when I flip from view to edit but then all the child form
components would need to be reassigned as I'm not rebuilding the view...

Chambre

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664368.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Submitting values for null associations

2014-02-11 Thread ChambreNoire
I forgot to mention, the reason I don't systematically make a new Job
instance is that when saving the form hibernate creates a new Job instance
which is effectively empty if no Job details have been input.

Chambre

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664369.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Submitting values for null associations

2014-02-11 Thread francois meillet
As Hibernate save a new Job instance when you save the User if the relation
is empty
why don't you load the job (with an eager fetching strategy) when you load
the user ?

François


On Tue, Feb 11, 2014 at 3:22 PM, ChambreNoire a...@tentelemed.com wrote:

 I forgot to mention, the reason I don't systematically make a new Job
 instance is that when saving the form hibernate creates a new Job instance
 which is effectively empty if no Job details have been input.

 Chambre

 --
 View this message in context:
 http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664369.html
 Sent from the Users forum mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org




Re: Submitting values for null associations

2014-02-11 Thread ChambreNoire
Why would that make any difference? A null Job is still a null Job. Unless
I'm missing something...

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664371.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Submitting values for null associations

2014-02-11 Thread francois meillet
You use cascade=all on the job relation.
When job is created, it should have at least one non-empty property.
Job's instance is null because it doesn't have any non empty property.

François


On Tue, Feb 11, 2014 at 3:59 PM, ChambreNoire a...@tentelemed.com wrote:

 Why would that make any difference? A null Job is still a null Job. Unless
 I'm missing something...

 --
 View this message in context:
 http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664371.html
 Sent from the Users forum mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org




Re: Submitting values for null associations

2014-02-11 Thread ChambreNoire
I'm afraid I still don't see it. I think I'm just going to have to use a
nested model at the form level and rebuild it whenever I toggle
viewing/editing mode. So I'd build a new Model() from the unproxied User
when switching to edit mode and build a new
AbstractEntityModelUser(user.id, User.class) when switching to viewing.
I'd also have to check for empty associations and null then upon saving so
as not to have empty associations (rows with just an id) in the db. Seems
rather painstaking but I can think of a better way atm...

Chambre

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664373.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Submitting values for null associations

2014-02-11 Thread Marios Skounakis
I'm not sure there is a very clean solution.

When switching to edit mode, you could set the User's Job to a new Job
instance if it is null. You don't have to reattach/reassign any form
components if you simply update the AbstractEntityModel's object. I.e. you
either do
model.getModelObject().setJob(new Job())
or you do model.setModelObject(user with non-null job)

You will of course have to revert the job property back to null before
saving if all its properties are empty.

I would implement a Job#isEmpty() method to help me decide if I need to set
user.job to null before saving to the db.

Note you may also have to delete an existing Job record if the user goes
ahead and updates it's name to null.

Marios



On Tue, Feb 11, 2014 at 4:22 PM, ChambreNoire a...@tentelemed.com wrote:

 I forgot to mention, the reason I don't systematically make a new Job
 instance is that when saving the form hibernate creates a new Job instance
 which is effectively empty if no Job details have been input.

 Chambre

 --
 View this message in context:
 http://apache-wicket.1842946.n4.nabble.com/Submitting-values-for-null-associations-tp4664365p4664369.html
 Sent from the Users forum mailing list archive at Nabble.com.

 -
 To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
 For additional commands, e-mail: users-h...@wicket.apache.org