:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

End users interact with link:https://causeway.apache.org/userguide/${CAUSEWAY_VERSION}/fun/overview.html#view-models[view models] in the same way as domain entities.

However, whereas domain entities are mapped to a datastore, view models are not.
Instead they are recreated dynamically by serializing their state, typically into the URL itself.

The framework provides a number of ways to serialize this state, but the most flexible and standard is to use JAXB annotations:

[source,java]
----
include::StatefulVmUsingJaxb.java[tags=class]
----

<.> mandatory, specifies this is a JAXB element
<.> mandatory boilerplate
<.> serializes the state by reading the field
<.> no further JAXB annotations required for the property.

Notice how the 'message' property can be modified and survives a refresh of the page.

Behind the scenes, the object is converted into an XML serialization, and that is URL encoded.


== Collections

JAXB also handles serializing graphs of data, which means that view models can also have collections of other objects:

[source,java,indent=0]
----
include::StatefulVmUsingJaxb.java[tags=child]
----

The demo provides a couple of actions to allow objects to be added and removed.

[source,java,indent=0]
----
include::StatefulVmUsingJaxb.java[tags=addChild]
----

NOTE: the child object has value-type semantics (is annotated with `@lombok.Data`).


== Long URLs

As you can see this can result in long URLs.
If the URL is too long, or needs to be encrypted, then the link:https://causeway.apache.org/refguide/${CAUSEWAY_VERSION}/applib-svc/UrlEncodingService.html[UrlEncodingService] SPI can provide for different serializations.

