It works! Here is my tested code. I'm doing a session.flush(), assuming database changes need to be applied before trying to pull the object again.
Code:
public static <T> T changeObjectType(
Session session, T object, Long id,
Class<T> genericType, String idColumn,
String discriminatorColumn, String discriminatorValue) {
session.evict(object);
Query query = session.createQuery(
"update " + genericType.getName() + " " +
"set " + discriminatorColumn + " = :newType " +
"where " + idColumn + " = :id)");
query.setLong("id", id);
query.setString("newType", discriminatorValue);
query.executeUpdate();
session.flush();
object = (T) session.get(genericType, id);
return object;
}
Though it would be way better to have a session.changeType(Object obj, Class newType). Much more portable. If anyone dares to post it as a JIRA issue for that, go ahead.