I have an entity. And sometimes I need this object, which also contains some value, call it "depth". The request may look like 'select b.id, b.name, b..., count(c.id) as depth from Entity b, CEntity c where ...'. So I created a NonHibernateEntity class that extends Entity. And then the query results written above are perfectly saved as a List of NonHibEntity, which consists of all Entity fields (as they expand) and the "depth" property. I do this by installing the aliasToBean result transformer: .setResultTransformer(Transformers.aliasToBean(NHEntity.class)).
But, it is annoying and inconvenient to specify all the aliases of all the required fields. And then, if I want to save one of this object in DB - session.saveOrUpdate((Enity)nHibEntity)- there is an exception from nHibEntity, it is not Hibernate Entity.
I heard about saving "entity" as a field in NonHibEntity (aggregation, not inheritance). But it seems that this is also inconvenient. What do you think? What is the right solution?
source
share