Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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

<transient name="foo" />
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top