You can either map the class twice and distinguish between the two mappings via the entity-name attribute of the class element, or write your own generator class (see refdocs section 5.1.4.1 and elsewhere) which does something fancy like:
Code:
public class SometimesIncrementGenerator extends org.hibernate.id.IncrementGenerator
{
@Override
public synchronized Serializable
generate(SessionImplementor session, Object object)
{
final Serializable id =
session.getEntityPersister(entityName, obj)
.getIdentifier(obj, session.getEntityMode());
if (id == nulll || id == -1)
{
return super.generate(session, object);
}
return id;
}
}
I don't know if autoboxing can handle Serializable, you may have to do something else to get the value from the id variable, probably involving instanceof and casting. That's left as an exercise for the reader :)