Hi Everyone--
I'm using hibernate on top of a legacy schema. In order to map the denormalized schema to the domain objects, in some cases I'm using custom property accessors. For instance, let's say my schema is as follows:
CREATE TABLE stats (
ID int primary key auto_increment,
`timestamp` datetime not null,
num1 bigint not null,
num2 bigint not null,
...
);
Now in the mapping, I have something like the following:
<class name="Stats">
<id>...</id>
<propery name="timestamp" type="timestamp"/>
<property name="num1" type="long" access="StatsPropAccessor"/>
<property name="num2" type="long" access="StatsPropAccessor"/>
...
</class>
Note that the Stats class *doesn't* have num1/num2 properties... There is a "num" List with two entries in it... StatsPropAccessor deals with this disconnect between the domain model and the legacy schema.
Now I have a query that aggregates records, similar to the following:
SELECT s.timestamp, sum(s.num1) as num1, sum(s.num2) as num2 FROM Stats s GROUP BY day(s.timestamp)
I'd like to get the results back as Stats instances. What is the best way of doing this? I assumed the following would work:
Query query = session.createQuery([Query as above]);
query.setResultTransformer(new AliasToBeanResultTransformer(Stats.class));
List<Stats> stats = (List<Stats>) query.list();
The problem is, AliasToBeanResultTransformer doesn't appear to take into consideration the custom PropertyAccessors that I've specified in the mapping. Instead, it's looking for fields/setters for num1 and num2, which obviously don't exist.
Note that simple queries such as "from Stats" do work, and query.list() returns Stats instances (i.e. the custom PropertyAccessor is functioning correctly).
Any insights into this issue, or alternative approaches, would be greatly appreciated. Thanks in advance!
Derek
|