Java Reference
In-Depth Information
{
if( entity instanceof Booking ) {
Booking booking = (Booking)entity;
booking.setName("unknown");
}
// The flag can't save us from ourselves here!
return false;
}
Again, this is probably not the best way to make the changes, but it can be useful when
you already have a considerable body of logic prepared to process the entity type.
Overriding the Default Constructor
Occasionally, you will find that it is necessary to persist a POJO that has no default constructor.
Usually you will have access to the source code, and should just make the change directly. Occa-
sionally, however, you may find that you are working with classes for which the source code is
not available—or that you are working with large bodies of generated objects for which it is
extremely inconvenient to manage changes made to the source code. In these circumstances,
it is possible to use an interceptor to replace the default object-creation logic.
This technique can be used as long as you have some way of obtaining or applying default
values for the parameters of the POJO's non-default constructor. Listing A-30 shows an exam-
ple of the use of this technique to instantiate a POJO whose only constructor demands a
String parameter.
Listing A-30. Invoking a Non-Default Constructor
private static class OverrideCtor implements Interceptor {
public Object instantiate(
String entityName,
EntityMode entityMode,
Serializable id)
throws CallbackException
{
if( entityName.equals(MissingDefaultCtorClass.class.getName())) {
// My call to CTor
return new MissingDefaultCtorClass("NOT SET");
} else {
// Some other class - continue to default handling
return null;
}
}
// ... the remaining default method declarations...
}
Search WWH ::




Custom Search