I just ran into the issue with my custom Interceptor....
After browsing through the source code I noticed that with SessionImpl.java has the following instantiate method:
public Object instantiate(EntityPersister persister, Serializable id) throws HibernateException {
Object result = interceptor.instantiate( persister.getEntityName(), id );
if ( result == null ) result = persister.instantiate( id );
return result;
}
This is great that I can create my own intantiate my the type of object that I need, but it does not allow me to set the identifier in a flexible manner.
Is there a reason why just the entityName is being passed into the interceptor's instantiate method and not the entire persister. The argument I make for doing the latter is that it allows you access to the setter that one defines for the identifier in the mapping file. Another alternative to this would be to introduce a new interceptor method, "setIdentifier(...)" that would allow you do such a thing.
The javadoc should be updated if the interceptor is expected to set the identifier for the new object being created in this method.
-------------------------------------
Mapping File
-------------------------------------
<dynamic-class entity-name="category" table="categories">
<id name="id" column="id" type="long" unsaved-value="0" access="org.progeeks.meta.hibernate.MetaObjectAccessor">
<generator class="native">
<param name="sequence">category_seq</param>
</generator>
</id>
<property name="parent CategoryId"
column="parent_category_id"
type="long"
insert="true"
update="true"
access="org.progeeks.meta.hibernate.MetaObjectAccessor"/>
many more properties defined in a similar way
</dynamic-class>
-------------------------------------
Custom Interceptor -- ( Implemented methods )
-------------------------------------
public class MetaObjectInterceptor implements Interceptor {
public Object instantiate( String name, Serializable serializable ) throws CallbackException
{
MetaClass metaClass = MetaClass.forName( name );
MetaObjectFactory factory = metaKit.getMetaObjectFactory();
MetaObject metaObject = factory.createMetaObject( metaClass );
return metaObject;
}
public String getEntityName( Object o ) throws CallbackException
{
MetaObject metaObject = ( MetaObject ) o;
return metaObject.getMetaClass().getName();
}
}
_________________ Matt Veitas
|