I'm looking to load data into a hibernate entity by running something like select * from <table> where id = ?
. I'm only interested in the data sitting in <table>
-- no joins whatsoever.
I can achieve this by using the following:
T result = (T) getSession()
.createSQLQuery("SELECT * FROM " + tableName + " WHERE " + idColumn + " = ?")
.setInteger(0, id)
.setResultTransformer(new AliasToBeanResultTransformer(type))
.uniqueResult();
But this only works for entities that don't have relationships. For any entity with a relationship to another, i get a PropertyNotFoundException
when the result transformer is trying to set some <relationshipName>_id
property on my entity (eg. org.hibernate.PropertyNotFoundException: Could not find setter for administration_id on class com.xxxxxx.xxxx.model.AdministrationPIN
Are there any other transformers that could automatically handle this. Or possibly some sort of object binder that could be used from an entity map, if I was to use the Criteria.ALIAS_TO_ENTITY_MAP
result transformer instead?
Update
It's important that all of the data sitting in the table is loaded into the entity, including the relationship data. So if entity A
has a OneToOne with entity B
, and I load entity A
using this method, a.getB()
should return an instance of B
, but the B
instance should only be populated with its identifier.