I have an object that is mapped to Hibernate using a hbm.xml file. The BEAN/object for the table includes an extra field, with appropriate getter/setters. I want to do something like:
SELECT T.*, 'XYZ' as otherData FROM table T
and have all the data mapped into the BEAN, including the value of the fake/additional column - 'otherData.'
This should be simple and maybe I'm missing something easy, but I cant get it to work. I've tried using
createSQLquery() with addEntity(), addScalar(), setResultTransformer()
and
createCriteria() with a projectionList() and setResultTransformer()
and whatever else I can think of and nothing gives the desired results. I dont want a generic list of Objects that I have to parse through to build my objects, and I don't want to have to specify every column of the table while building this query.
Is there any way this can be done?
You might consider using a formula for the property. You can fill this with any SQL query data. Consider for example an order - order line relation where you want to fill a property of the order with the number of order lines, like:
public class Order {
private String orderNumber;
private int lineCount;
... }
then the mapping would be:
<class name="Order" ....>
...
<property name="lineCount"
formula="(select COUNT(*) from ORDER_LINE l where l.ORDER_NUMBER = ORDER_NUMBER)"/>
...
</class>