Here is my first solution, after a few hours thinking about the best way to solve it (I'm not saying this is THE best way, but it works, at least :) ) :
level is an enum
each value is defined in the right order in the Level enum class : VALUE1 first, then VALUE2, and so on.
Then I use this kind of query for a "findAllForLevel" need :
from ObjectA o where o.level = (select max(level) from ObjectA o2 where o2.key = o.key and o2.level <= :level)
And this one if I want the first "node" matching my need for a
particular key :
from ObjectA o where
o.key = :key and o.level = (select max(o2.level) from ObjectA o2 where o2.key = o.key and o2.level <= :level)
Since enums can be compared to each other depending on their ordinal position, it works.
From the Java doc :
Quote:
Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented by this method is the order in which the constants are declared
Any feedback regarding this approach ?