views:

40

answers:

2

I have a property of a business object which is calculated. The calculation involves some of the logged in user's details, and so can't be represented as a simple SQL query.

I'm having trouble representing the field in the Hibernate mapping XML file, because Hibernate continues to try and retrieve the field from the database, although there is no column by that name.

A: 

I'm having trouble representing the field in the Hibernate mapping XML file, because Hibernate continues to try and retrieve the field from the database, although there is no column by that name.

I'm probably missing something obvious but... why are you mapping this field then? But just in case, this might be a good use case for a <formula> attribute.


The field is in the Java class, and when I code a query I can provide the custom SQL to fill it, e.g: SELECT t.*, (SELECT x FROM y) AS field FROM table t WHERE... Hibernate allows me to then use SQLQuery.addEntity(Z.class) and creates the object appropriately. As far as I understand, if I leave the field out of the mapping altogether, I can never fill that attribute through my query. I have tried :)

I was suspecting something like this but I wanted to get confirmation. I don't think you have that many options here: either use a <formula> (be it a "dumb" formula just to trick hibernate) or return a non managed entity.

Pascal Thivent
The field is in the Java class, and when I code a query I can provide the custom SQL to fill it, e.g:SELECT t.*, (SELECT x FROM y) AS field FROM table t WHERE...Hibernate allows me to then use SQLQuery.addEntity(Z.class) and creates the object appropriately.As far as I understand, if I leave the field out of the mapping altogether, I can never fill that attribute through my query. I have tried :)
Fry
Ok so I tried having both a dumb formula and the overriding complex query, and I receive the following NPE: http://pastebin.com/1euDAGeMWhich is this bug here: http://stackoverflow.com/questions/3331529/native-sql-query-for-an-hibernate-entity-using-formula-results-in-nullpointerexc
Fry
@Fry: Argh! Looks like the "trick" won't work, at least not right now...
Pascal Thivent
So you're saying there's absolutely nothing I can do to get this field to work?
Fry
@Fry There is something: fix the bug :)
Pascal Thivent
A: 

It sounds like you want this property to be "transient"

<transient name="foo" />
walnutmon
This is not an element of `hbm.xml` (AFAIK, any non mapped property is "transient" by definition since Hibernate won't be aware of it).
Pascal Thivent