-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Retrieving latest entry in a list
PostPosted: Thu Jan 25, 2007 5:00 pm 
Newbie

Joined: Wed Jan 24, 2007 3:37 pm
Posts: 4
I have a list that represents the state transitions in a document workflow. I want to add a method to my DAO that will retrieve all documents that have a particular state, but can't work out the HQL to do this. My environment is Java 5, Oracle 9i, Hibernate 3.2. Here are the relevant snippets of annotated Java:

Code:
@Entity
@Table(name="aml_institution")
public class Institution
    implements Serializable {
    ...
    @Id
    @Column
    private int id;
    ...
    @Embedded
    private RiskAssessment riskAssessment;
    ...
}

@Embeddable
public class RiskAssessment {
    ...
    @CollectionOfElements
    @JoinTable(name="aml_transition",
        joinColumns = @JoinColumn(name="institution_id"))
    @IndexColumn(name="ndx", base=0)
    private List<Transition> transitions;
    ...
}

@Embeddable
public class Transition {
    /** state resulting from this transition */
    @Enumerated(EnumType.STRING)
    @Column(name="state", length=20, nullable=false)
    private State resultingState;

    @ManyToOne
    @JoinColumn(name="user_id", nullable=false)
    private User who;

     /** time the action was performed */
    @Column(name="time_of", nullable=false)
    private Date when;

     /** any comment entered by the user */
    @Column(name="comments", length=500)
    private String comment;
    ...
}

I first thought a query with a group by clause would do the trick, and tried the following in the Hibernate Console:
Code:
select i, max(tran.when)
from Institution i
join i.riskAssessment.transitions tran
where tran.resultingState = 'PENDING'
group by i

but Hibernate complains, throwing an SQLGrammarException: could not execute query. A variation of that query
Code:
select i.id, max(tran.when)
from Institution i
join i.riskAssessment.transitions tran
where tran.resultingState = 'PENDING'
group by i.id

does execute and the resulting ids are correct.
An alternative might be to use a subquery to retrieve the transition with the maximum timestamp for each Institution, but I'm not sure how to do this. Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 6:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
select i
from Institution i
join i.riskAssessment.transitions tran
where tran.resultingState = :state
  and tran.when = (select max(tr.when)
                   from Institution j
                   join j.riskAssessment.transitions tr
                   where j.id = i.id
                     and tr.resultingState = :state)

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 10:14 am 
Newbie

Joined: Wed Jan 24, 2007 3:37 pm
Posts: 4
Thank you tenwit, that seems to work nicely.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.