title: Wicket - Serialization of persisted and non-persisted JPA entities
body: <p>I know that when using Wicket with JPA frameworks it is not advisable to serialize entities that have already been persisted to the database (because of problems with lazy fields and to save space). In such cases we are supposed to use LoadableDetachableModel. But what about the following use-case?</p>
  
  <p>Suppose we want to create a new entity (say, a Contract) which will consist, among other things, of persisted entities (say, a Client which is selected from a list of clients stored in the DB). The entity under creation is a model object of some Wicket component (say, a Wizard). In the end (when we finish our wizard) we save the new entity to the DB. So my question is: what is the best generic solution to the serialization problem of such model objects? We can't use LDM because the entity is not in the DB yet but we don't want our inner entities (like Client) to be serialized wholly, too.</p>
  
  <p>My idea was to implement a custom wicket serializer that checks if the object is an entity and if it is persisted. If so, store only its id, otherwise use the default serialization. Similarly, when deserializing use the stored id and get the entity from the DB or deserialize using the default mechanism. Not sure, though, how to do that in a generic way. My next thought was that if we can do it, then we do not need any LDM anymore, we can just store all our entities in simple org.apache.wicket.model.Model models and our serialization logic will take care of them, right?</p>
  
  <p>Here's some code:</p>
  
  <pre><code>  @Entity
    Client {
       String clientName;
  
       @ManyToOne(fetch = FetchType.LAZY)
       ClientGroup group;
    }
  
    @Entity
    Contract {
       Date date;
  
       @ManyToOne(fetch = FetchType.LAZY)
       Client client;
    }
  
    ContractWizard extends Wizard {
       ContractWizard(String markupId, IModel&lt;Contract&gt; model) {
          super(markupId);
          setDefaultModel(model);
       }
    }
  
    Contract contract = DAO.createEntity(Contract.class);
    ContractWizard wizard = new ContractWizard("wizard", ?); 
  </code></pre>
  
  <p>How to pass the contract? If we just say Model.of(contract) the whole contract will be serialized along with inner client (and it can be big), moreover if we access contract.client.group after deserialization we can bump into the problem: <a href="https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Serialization.2C_and_Detaching" rel="nofollow">https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Serialization.2C_and_Detaching</a></p>
  
  <p>So I wonder how people go about solving such issues, I'm sure it's a fairly common problem.</p>