Hibernate -- Mapping a view with a single value to a read-only field
(Hibernate 3.6, Mysql 5)
I have a mapped class called 'Program' (that is working correctly).
I also have a view that is updated 开发者_C百科daily with aggregate statistics.
The view schema is like so:
view ProgramSummary {
long program_id
long value
}
program_id is unique, but there is no row-id. What I want is to just pull the value out, but I can't seem to figure out the right semantics.
I have tried mapping a separate entitiy with a OneToOne (and ManyToOne) relationship, but it gets confused between the ids. Without marking program_id as an entity, hibernate can't handle the actual mapping.
Is there a simple way to do this? It is a read-only field that is accessed by an external process and I don't have any leeway for changing the schema.
You can use a formula in your Program hbm file to map this.
<property name="SummaryValue" type="long" formula="(SELECT value FROM ProgramSummary WHERE ProgramSummary.program_id = id)" />
Assuming your field is named SummaryValue this line will update that field in the Program object. Any field mapped with a formula is not updateable.
If you map a second entity, you might want to set the columns to updatable=false
and insertable=false
, making them readonly.
However, why don't you just select the value from ProgramSummary, probably with plain SQL?
精彩评论