0

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.

jlb
  • 19,090
  • 8
  • 34
  • 65
  • It looks like simple query, cant you just use HQL insteed of native SQL? As for your update, I don't think it is possible. However maybe this could help you a little bit http://stackoverflow.com/questions/25885310/jpa-hibernate-how-to-get-fk-of-child-entity-without-fetching-that-entity – Antoniossss Sep 30 '14 at 09:03
  • Would using HQL prevent table joins? – jlb Sep 30 '14 at 09:08
  • It depends on what kind of `FETCH` policy you have delcared. If it is always `LAZY` than yes, no joins nor aditional selects will be performed. – Antoniossss Sep 30 '14 at 09:09
  • Indeed, but I would like this to function irrespective of the ``FETCH`` policy - for example, OneToOne relationships take special configuration to be set as ``LAZY`` – jlb Sep 30 '14 at 09:14
  • It sould be the other way around. All of your associations should be set to `LAZY` fetching and they should be fetched when needed with `FETCH` mode of the query. There is no way to disable `EAGAR` fetching declared im mapping files or annotations (I haven't heard of such) – Antoniossss Sep 30 '14 at 09:16
  • I disagree that all associations should be set to ``LAZY`` - that is a more application-specific configuration, which does not suit my needs. – jlb Sep 30 '14 at 09:31
  • You cannot disagree that if it would be lazy, than it would be more flexible, as you could always force H to fetch lazy associations eagarly – Antoniossss Sep 30 '14 at 09:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62168/discussion-between-jlb-and-antoniossss). – jlb Sep 30 '14 at 09:42

0 Answers0