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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Fry
  • 23
  • 2

2 Answers2

1

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 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 Aug 19 '10 at 01:33
  • Ok so I tried having both a dumb formula and the overriding complex query, and I receive the following NPE: http://pastebin.com/1euDAGeM Which is this bug here: http://stackoverflow.com/questions/3331529/native-sql-query-for-an-hibernate-entity-using-formula-results-in-nullpointerexc – Fry Aug 19 '10 at 04:27
  • So you're saying there's absolutely nothing I can do to get this field to work? – Fry Aug 23 '10 at 00:19
  • @Fry There is something: fix the bug :) – Pascal Thivent Aug 23 '10 at 00:31
0

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

<transient name="foo" />
walnutmon
  • 5,873
  • 7
  • 42
  • 57
  • 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 Aug 18 '10 at 18:22