1

I have a relation between element and its names. All historical names as well as the current one are located in table "element_name" that has field "created". The row last created is the current name of the element.

How could I map the current name of the element as the property of the element?

class Element implements Serializable {

    @OneToMany(fetch = FetchType.LAZY)
    private List<ElementName> historyOfElementNames;

    // What annotations should be used here?
    private ElementName currentElementName;

    ...
}

Thanks in advance!

Mikk
  • 551
  • 4
  • 9

1 Answers1

0

An alternative to your solution would be to map all names in a list, sort that list and then get the current name as elementNames.get(elementNames.size() - 1).

To enable this, add the @IndexColumn annotation as well as the actual index column and indices. That way you also get the order of name changes.

Edit: as of Hibernate 3.5 @IndexColumn seems to have been renamed to @OrderColumn.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • @user966810 Well, I doesn't seem as if there's an easy way to achieve this using annotations only. You could make `currentElementName` a collection and apply a filter (`@Filter` annotation) to only select the newest. Since you're mapping the two properties to one table/column, keep in mind that one would need to be read-only (`insertable = false, updatable = false`). – Thomas Sep 27 '11 at 14:21
  • That would work in some cases, but that way I'm probably not able to filter bunch of Elements according to the name (actually my element has the same idea over its state, which is usually condition in the clause). I almost achieved what I need by using Formula annotation, but it turns out that Formula can only be used for mapping a field not an object. – Mikk Sep 27 '11 at 14:21