1

I'm working in a Java application using Hibernate 3.6.9 as ORM and xml mappings. Actually, having two entities, Detectable and Revision, Detectable has a one-to-many relation with Revision, having its entire set of done revisions mapped:

<set name="_Revisions" table="trevision" inverse="true" lazy="true">
    <key>
        <column name="id_detectable" />
    </key>
    <one-to-many class="com.company.model.tasks.Revision" />
</set>

Refer also to this similar question I posted. What I want now is to get the last of that revisions in a separate field of the class:

<many-to-one name="_LastDoneRevision" column="id_detectable"
            class="com.company.model.tasks.Revision" lazy="false" />

What I want to achieve with that is to have the last revision (which I use in most of the cases) always eagerly fetched. Apart from that I could get the whole set of Revisions if I want, but it's lazily initialized by default. To achieve this, it might be necessary to use a custom query to get the last revision done from the whole set.

I think that can't be achieved with formulas, because they just retrieve a value, not a whole Entity. I want to be able in some way to choose what the many-to-one relation should have.

A solution could be to use a view instead of the Detectable table itself. That way I could have a VDetectable entity which would be mapped against the view and the view could obtain all the original attributes plus the reference to the Revision I'm interested in.

Anyway, is it possible to have it loaded using only the original entity?

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217

2 Answers2

3

I dont promise its efficient or even the right way to do it, but I have done a similar thing like this...

<many-to-one 
  name="theMaxSetting"
  class="myPath.domain.Setting"             
  formula="(select MAX(s.SETTING_ID) from setting s where s.profile_id = PROFILE_ID)"
/> 

In my case, a profile has many settings. The formula is a bit tricky to get right, but basically the formula returns a number, and hibernate uses that number to lookup the corresponding entity.

Just to state the obvious in case ... Profile.java now contains

private Setting theMaxSetting;
//....
//getter / setter

This link shows various examples of the use of formula. Some examples demonstrate how it is used with sql queries, others demonstrate that it can be used to return an entity. I dont think there is one that combines them into a single use case, but all the pieces are there.

user2046211
  • 356
  • 1
  • 4
  • 20
  • Thanks for your answer. I'll be grateful if you would be able to provide some reference in the web where this method is shown being applied. – Aritz Aug 27 '14 at 20:43
  • Thanks for the link, anyway I didn't know a formula could be used in that way (if you take a look to the reference, it only shows examples with entity properties and not the id itself). It works properly, I'm just going to wait if anybody provides a different way. – Aritz Aug 29 '14 at 09:25
1

Yes, a formula is the solution you want. Just provide Hibernate with a formula that computes the primary key of the entity you want to link. I guess your xml will look like:

<many-to-one name="_LastDoneRevision"
    class="com.company.model.tasks.Revision" lazy="false">
  <formula>
    (select max(r.id_revision)
    from revision r
    where r.id_detectable = id_detectable)
  </formula>
</many-to-one>
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36