I have a class (Cheque) with a temporal collection (situacoes). Most often, I just need to access the
most current object in this collection, but the whole collection is fetched when I call it's
getCurrentSituacao() method. Is there some way to instruct Hibernate to fetch only the most current object in this temporal collection?
Mapping:
Code:
<hibernate-mapping>
<class table="cheque" name="Cheque">
<cache usage="read-write"/>
<id column="id" name="id">
<generator class="native"/>
</id>
<bag name="situacoes"
inverse="false"
cascade="all"
order-by="start_date"
>
<cache usage="read-write"/>
<key column="cheque_id"/>
<one-to-many class="Situacao"/>
</bag>
</class>
<typedef name="EstadoUserType" class="EnumUserType">
<param name="enumClassName">Estado</param>
<param name="columnType">string</param>
</typedef>
<class table="situacoes" name="Situacao">
<cache usage="read-write" />
<id column="id" name="id">
<generator class="native"/>
</id>
<property name="startDate"
type="timestamp"
column="start_date"
not-null="true"
/>
<property name="endDate"
type="timestamp"
column="end_date"
not-null="false"
/>
<many-to-one
column="cheque_id"
unique="true"
not-null="true"
name="cheque"
class="Cheque"
/>
<property name="estado"
column="estado"
type="EstadoUserType"
not-null="true"
/>
</class>
</hibernate-mapping>
Classes:
Code:
public class Cheque {
private Long id;
private List<Situacao> situacoes;
public Cheque() {
situacoes = new LinkedList<Situacao>();
Situacao situacaoInicial = new SituacaoImpl(this);
situacoes.add(situacaoInicial);
}
public Situacao getCurrentSituacao() {
return situacoes.get(situacoes.size() - 1);
}
public List<Situacao> getSituacoes() {
return situacoes;
}
public void setSituacoes(List<Situacao> situacoes) {
this.situacoes = situacoes;
}
public Estado getCurrentEstado() {
return this.getCurrentSituacao().getEstado();
}
}
I've ommitted the Situacao and Estado classes for brevity since I think they're not relevant to this problem.
Thanks in advance,
Daniel Serodio